parse-community / parse-community/parse-server

Modernize Codebase

Open
#7,563 7 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

type:feature
Dominant language
JavaScript
Stars
21.4k
Forks
4.8k
Avg merge
7h 45m
Merged PRs (30d)
11

Description

New Feature / Enhancement Checklist
Current Limitation

Sometimes, contributing can be really hard as some of the internal code was written in 2015, without the uses of await/async or modern JS features. The hardest part can be getting your head around how the code works.

Feature / Enhancement Description

Modernize codebase and unit tests to make future contributing easier. A lot of the unit tests could be written in much less lines using expectAsync.

Would obviously have to be a progressive, file-by-file approach with minimal interruptions.

An example is the function that handles cloud functions:

static createResponseObject(resolve, reject) {
    return {
      success: function (result) {
        resolve({
          response: {
            result: Parse._encode(result),
          },
        });
      },
      error: function (message) {
        const error = triggers.resolveError(message);
        reject(error);
      },
    };
  }
  static handleCloudFunction(req) {
    const functionName = req.params.functionName;
    const applicationId = req.config.applicationId;
    const theFunction = triggers.getFunction(functionName, applicationId);

    if (!theFunction) {
      throw new Parse.Error(Parse.Error.SCRIPT_FAILED, `Invalid function: "${functionName}"`);
    }
    let params = Object.assign({}, req.body, req.query);
    params = parseParams(params);
    const request = {
      params: params,
      master: req.auth && req.auth.isMaster,
      user: req.auth && req.auth.user,
      installationId: req.info.installationId,
      log: req.config.loggerController,
      headers: req.config.headers,
      ip: req.config.ip,
      functionName,
      context: req.info.context,
    };

    return new Promise(function (resolve, reject) {
      const userString = req.auth && req.auth.user ? req.auth.user.id : undefined;
      const cleanInput = logger.truncateLogMessage(JSON.stringify(params));
      const { success, error } = FunctionsRouter.createResponseObject(
        result => {
          try {
            const cleanResult = logger.truncateLogMessage(JSON.stringify(result.response.result));
            logger.info(
              `Ran cloud function ${functionName} for user ${userString} with:\n  Input: ${cleanInput}\n  Result: ${cleanResult}`,
              {
                functionName,
                params,
                user: userString,
              }
            );
            resolve(result);
          } catch (e) {
            reject(e);
          }
        },
        error => {
          try {
            logger.error(
              `Failed running cloud function ${functionName} for user ${userString} with:\n  Input: ${cleanInput}\n  Error: ` +
                JSON.stringify(error),
              {
                functionName,
                error,
                params,
                user: userString,
              }
            );
            reject(error);
          } catch (e) {
            reject(e);
          }
        }
      );
      return Promise.resolve()
        .then(() => {
          return triggers.maybeRunValidator(request, functionName, req.auth);
        })
        .then(() => {
          return theFunction(request);
        })
        .then(success, error);
    });
  }

Example Use Case
Alternatives / Workarounds

Leave as is

3rd Party References

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the FunctionsRouter.handleCloudFunction example and the unit tests around it. Review how the current promise chains and callbacks could be modernized, then identify a narrowly scoped file-by-file change using async/await or expectAsync. Done is not defined for the broader codebase modernization, so the specific target and tests must be agreed first.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, nodejs
Domain
backend, developer-experience, testing
Issue type
Refactor
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.