crypto#randomUUID TypeScript Examples

The following examples show how to use crypto#randomUUID. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: shell.ts    From command-bot with Apache License 2.0 6 votes vote down vote up
constructor(
    ctx: Context,
    private configuration: {
      itemsToRedact: string[]
      shouldTrackProgress?: boolean
      cwd?: string
      onChild?: (child: ChildProcess) => void
    },
  ) {
    this.logger = ctx.logger.child({ commandId: randomUUID() })
  }
Example #2
Source File: create-secrets.ts    From airnode with MIT License 5 votes vote down vote up
createSignedDataGatewayApiKey = (generateExampleFile: boolean) => {
  if (generateExampleFile) return '17819376-9f6c-43c7-b6cf-3ffb114ae864';

  return randomUUID();
}
Example #3
Source File: create-secrets.ts    From airnode with MIT License 5 votes vote down vote up
createHttpGatewayApiKey = (generateExampleFile: boolean) => {
  if (generateExampleFile) return '17819376-9f6c-43c7-b6cf-3ffb114ae864';

  return randomUUID();
}
Example #4
Source File: task.ts    From command-bot with Apache License 2.0 5 votes vote down vote up
getNextTaskId = () => {
  return `${getNextUniqueIncrementalId()}-${randomUUID()}`
}
Example #5
Source File: api.ts    From command-bot with Apache License 2.0 4 votes vote down vote up
setupApi = (ctx: Context, server: Server) => {
  const { accessDb, matrix, repositoryCloneDirectory, logger, gitlab } = ctx

  const apiError = (res: Response, next: NextFunction, error: unknown) => {
    const msg = "Failed to handle errors in API endpoint"
    logger.fatal(error, msg)
    errorResponse(res, next, 500, msg)
  }

  type JsonRequestHandlerParams = Parameters<
    RequestHandler<Record<string, unknown>, unknown, Record<string, unknown>>
  >
  const setupRoute = <T extends "post" | "get" | "delete" | "patch">(
    method: T,
    routePath: string,
    handler: (args: {
      req: JsonRequestHandlerParams[0]
      res: JsonRequestHandlerParams[1]
      next: JsonRequestHandlerParams[2]
      token: string
      matrixRoom: string
    }) => void | Promise<void>,
    { checkMasterToken }: { checkMasterToken?: boolean } = {},
  ) => {
    server.expressApp[method](
      getApiRoute(routePath),
      jsonBodyParserMiddleware,
      async (req, res, next) => {
        try {
          const token = req.headers["x-auth"]
          if (typeof token !== "string" || !token) {
            return errorResponse(res, next, 400, "Invalid auth token")
          }

          /*
            Empty when the masterToken is supposed to be used because it doesn't
            matter in that case
          */
          let matrixRoom: string = ""
          if (checkMasterToken) {
            if (token !== ctx.masterToken) {
              return errorResponse(
                res,
                next,
                422,
                `Invalid ${token} for master token`,
              )
            }
          } else {
            try {
              matrixRoom = await accessDb.db.get(token)
            } catch (error) {
              if (error instanceof LevelErrors.NotFoundError) {
                return errorResponse(res, next, 404, "Token not found")
              } else {
                return apiError(res, next, error)
              }
            }
          }

          await handler({ req, res, next, token, matrixRoom })
        } catch (error) {
          apiError(res, next, error)
        }
      },
    )
  }

  setupRoute("post", "/queue", async ({ req, res, next, matrixRoom }) => {
    if (matrix === null) {
      return errorResponse(
        res,
        next,
        400,
        "Matrix is not configured for this server",
      )
    }

    const validation = Joi.object()
      .keys({
        configuration: Joi.string().required(),
        args: Joi.array().items(Joi.string()).required(),
        variables: Joi.object().pattern(/.*/, [
          Joi.string(),
          Joi.number(),
          Joi.boolean(),
        ]),
        gitRef: Joi.object().keys({
          contributor: Joi.object().keys({
            owner: Joi.string().required(),
            repo: Joi.string().required(),
            branch: Joi.string().required(),
          }),
          upstream: Joi.object().keys({
            owner: Joi.string().required(),
            repo: Joi.string().required(),
          }),
        }),
      })
      .validate(req.body)
    if (validation.error) {
      return errorResponse(res, next, 422, validation.error)
    }

    const {
      configuration: configurationName,
      gitRef,
      args,
      variables,
    } = req.body as Pick<ApiTask, "gitRef"> & {
      configuration: string
      args: string[]
      variables?: Record<string, number | boolean | string>
    }

    const configuration =
      configurationName in commandsConfiguration
        ? commandsConfiguration[
            configurationName as keyof typeof commandsConfiguration
          ]
        : undefined
    if (!configuration) {
      return errorResponse(
        res,
        next,
        422,
        `Could not find matching configuration for ${configurationName}; available ones are ${Object.keys(
          commandsConfiguration,
        ).join(", ")}.`,
      )
    }

    const command = await validateSingleShellCommand(
      ctx,
      [...configuration.commandStart, ...args].join(" "),
    )
    if (command instanceof Error) {
      return errorResponse(res, next, 422, command.message)
    }

    const taskId = getNextTaskId()
    const queuedDate = new Date()

    const task: ApiTask = {
      id: taskId,
      tag: "ApiTask",
      timesRequeued: 0,
      timesRequeuedSnapshotBeforeExecution: 0,
      timesExecuted: 0,
      gitRef,
      matrixRoom,
      repoPath: path.join(repositoryCloneDirectory, gitRef.upstream.repo),
      queuedDate: serializeTaskQueuedDate(queuedDate),
      requester: matrixRoom,
      command,
      gitlab: {
        job: {
          ...configuration.gitlab.job,
          variables: { ...configuration.gitlab.job.variables, ...variables },
          image: gitlab.jobImage,
        },
        pipeline: null,
      },
    }

    const updateProgress = getSendTaskMatrixResult(matrix, logger, task)
    const queueMessage = await queueTask(ctx, task, {
      onResult: updateProgress,
      updateProgress,
    })

    response(res, next, 201, { task, queueMessage })
  })

  setupRoute("delete", taskRoute, async ({ req, res, next }) => {
    const { task_id: taskId } = req.params
    if (typeof taskId !== "string" || !taskId) {
      return errorResponse(res, next, 403, "Invalid task_id")
    }

    const cancelError = await cancelTask(ctx, taskId)
    if (cancelError instanceof LevelErrors.NotFoundError) {
      return errorResponse(res, next, 404, "Task not found")
    }

    response(res, next, 204)
  })

  setupRoute(
    "post",
    "/access",
    async ({ req, res, next }) => {
      const { matrixRoom } = req.body
      if (typeof matrixRoom !== "string" || !matrixRoom) {
        return errorResponse(res, next, 400, "Invalid matrixRoom")
      }

      const requesterToken = randomUUID()
      try {
        if (await accessDb.db.get(requesterToken)) {
          return errorResponse(res, next, 422, "requesterToken already exists")
        }
      } catch (error) {
        if (!(error instanceof LevelErrors.NotFoundError)) {
          return apiError(res, next, error)
        }
      }

      await accessDb.db.put(requesterToken, matrixRoom)
      response(res, next, 201, { token: requesterToken })
    },
    { checkMasterToken: true },
  )

  setupRoute("patch", "/access", async ({ req, res, next, token }) => {
    const { matrixRoom } = req.body
    if (typeof matrixRoom !== "string" || !matrixRoom) {
      return errorResponse(res, next, 400, "Invalid matrixRoom")
    }

    try {
      const value = await accessDb.db.get(token)
      if (!value) {
        return errorResponse(res, next, 404)
      }
    } catch (error) {
      if (error instanceof LevelErrors.NotFoundError) {
        return errorResponse(res, next, 404)
      } else {
        return apiError(res, next, error)
      }
    }

    await accessDb.db.put(token, matrixRoom)
    response(res, next, 204)
  })

  setupRoute("delete", "/access", async ({ res, next, token }) => {
    try {
      const value = await accessDb.db.get(token)
      if (!value) {
        return errorResponse(res, next, 404)
      }
    } catch (error) {
      if (error instanceof LevelErrors.NotFoundError) {
        return errorResponse(res, next, 404)
      } else {
        return apiError(res, next, error)
      }
    }

    await accessDb.db.put(token, "")
    response(res, next, 204)
  })
}