extensions.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*! firebase-admin v12.1.1 */
  2. "use strict";
  3. /*!
  4. * @license
  5. * Copyright 2022 Google Inc.
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. */
  19. Object.defineProperty(exports, "__esModule", { value: true });
  20. exports.Runtime = exports.Extensions = void 0;
  21. const extensions_api_client_internal_1 = require("./extensions-api-client-internal");
  22. const validator = require("../utils/validator");
  23. /**
  24. * The Firebase `Extensions` service interface.
  25. */
  26. class Extensions {
  27. /**
  28. * @param app - The app for this `Extensions` service.
  29. * @constructor
  30. * @internal
  31. */
  32. constructor(app) {
  33. this.app = app;
  34. this.client = new extensions_api_client_internal_1.ExtensionsApiClient(app);
  35. }
  36. /**
  37. * The runtime() method returns a new Runtime, which provides methods to modify an extension instance's runtime data.
  38. *
  39. * @remarks
  40. * This method will throw an error if called outside an Extensions environment.
  41. *
  42. * @returns A new {@link Runtime} object.
  43. */
  44. runtime() {
  45. return new Runtime(this.client);
  46. }
  47. }
  48. exports.Extensions = Extensions;
  49. /**
  50. * Runtime provides methods to modify an extension instance's runtime data.
  51. */
  52. class Runtime {
  53. /**
  54. * @param client - The API client for this `Runtime` service.
  55. * @constructor
  56. * @internal
  57. */
  58. constructor(client) {
  59. this.projectId = this.getProjectId();
  60. if (!validator.isNonEmptyString(process.env['EXT_INSTANCE_ID'])) {
  61. throw new extensions_api_client_internal_1.FirebaseExtensionsError('invalid-argument', 'Runtime is only available from within a running Extension instance.');
  62. }
  63. this.extensionInstanceId = process.env['EXT_INSTANCE_ID'];
  64. if (!validator.isNonNullObject(client) || !('updateRuntimeData' in client)) {
  65. throw new extensions_api_client_internal_1.FirebaseExtensionsError('invalid-argument', 'Must provide a valid ExtensionsApiClient instance to create a new Runtime.');
  66. }
  67. this.client = client;
  68. }
  69. /**
  70. * Sets the processing state of an extension instance.
  71. *
  72. * @remarks
  73. * Use this method to report the results of a lifecycle event handler.
  74. *
  75. * If the lifecycle event failed & the extension instance will no longer work
  76. * correctly, use {@link Runtime.setFatalError} instead.
  77. *
  78. * To report the status of function calls other than lifecycle event handlers,
  79. * use `console.log` or the Cloud Functions logger SDK.
  80. *
  81. * @param state - The state to set the instance to.
  82. * @param detailMessage - A message explaining the results of the lifecycle function.
  83. */
  84. async setProcessingState(state, detailMessage) {
  85. await this.client.updateRuntimeData(this.projectId, this.extensionInstanceId, {
  86. processingState: {
  87. state,
  88. detailMessage,
  89. },
  90. });
  91. }
  92. /**
  93. * Reports a fatal error while running a lifecycle event handler.
  94. *
  95. * @remarks
  96. * Call this method when a lifecycle event handler fails in a way that makes
  97. * the Instance inoperable.
  98. * If the lifecycle event failed but the instance will still work as expected,
  99. * call `setProcessingState` with the "PROCESSING_WARNING" or
  100. * "PROCESSING_FAILED" state instead.
  101. *
  102. * @param errorMessage - A message explaining what went wrong and how to fix it.
  103. */
  104. async setFatalError(errorMessage) {
  105. if (!validator.isNonEmptyString(errorMessage)) {
  106. throw new extensions_api_client_internal_1.FirebaseExtensionsError('invalid-argument', 'errorMessage must not be empty');
  107. }
  108. await this.client.updateRuntimeData(this.projectId, this.extensionInstanceId, {
  109. fatalError: {
  110. errorMessage,
  111. },
  112. });
  113. }
  114. getProjectId() {
  115. const projectId = process.env['PROJECT_ID'];
  116. if (!validator.isNonEmptyString(projectId)) {
  117. throw new extensions_api_client_internal_1.FirebaseExtensionsError('invalid-argument', 'PROJECT_ID must not be undefined in Extensions runtime environment');
  118. }
  119. return projectId;
  120. }
  121. }
  122. exports.Runtime = Runtime;