No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

storage.js 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.getServiceAccount = exports.listBuckets = exports.getBucket = exports.deleteObject = exports.uploadObject = exports.upload = exports.getDefaultBucket = void 0;
  4. const path = require("path");
  5. const api_1 = require("../api");
  6. const apiv2_1 = require("../apiv2");
  7. const logger_1 = require("../logger");
  8. const error_1 = require("../error");
  9. async function getDefaultBucket(projectId) {
  10. try {
  11. const appengineClient = new apiv2_1.Client({ urlPrefix: api_1.appengineOrigin, apiVersion: "v1" });
  12. const resp = await appengineClient.get(`/apps/${projectId}`);
  13. if (resp.body.defaultBucket === "undefined") {
  14. logger_1.logger.debug("Default storage bucket is undefined.");
  15. throw new error_1.FirebaseError("Your project is being set up. Please wait a minute before deploying again.");
  16. }
  17. return resp.body.defaultBucket;
  18. }
  19. catch (err) {
  20. logger_1.logger.info("\n\nThere was an issue deploying your functions. Verify that your project has a Google App Engine instance setup at https://console.cloud.google.com/appengine and try again. If this issue persists, please contact support.");
  21. throw err;
  22. }
  23. }
  24. exports.getDefaultBucket = getDefaultBucket;
  25. async function upload(source, uploadUrl, extraHeaders) {
  26. const url = new URL(uploadUrl);
  27. const localAPIClient = new apiv2_1.Client({ urlPrefix: url.origin, auth: false });
  28. const res = await localAPIClient.request({
  29. method: "PUT",
  30. path: url.pathname,
  31. queryParams: url.searchParams,
  32. responseType: "xml",
  33. headers: Object.assign({ "content-type": "application/zip" }, extraHeaders),
  34. body: source.stream,
  35. skipLog: { resBody: true },
  36. });
  37. return {
  38. generation: res.response.headers.get("x-goog-generation"),
  39. };
  40. }
  41. exports.upload = upload;
  42. async function uploadObject(source, bucketName) {
  43. if (path.extname(source.file) !== ".zip") {
  44. throw new error_1.FirebaseError(`Expected a file name ending in .zip, got ${source.file}`);
  45. }
  46. const localAPIClient = new apiv2_1.Client({ urlPrefix: api_1.storageOrigin });
  47. const location = `/${bucketName}/${path.basename(source.file)}`;
  48. const res = await localAPIClient.request({
  49. method: "PUT",
  50. path: location,
  51. headers: {
  52. "Content-Type": "application/zip",
  53. "x-goog-content-length-range": "0,123289600",
  54. },
  55. body: source.stream,
  56. });
  57. return {
  58. bucket: bucketName,
  59. object: path.basename(source.file),
  60. generation: res.response.headers.get("x-goog-generation"),
  61. };
  62. }
  63. exports.uploadObject = uploadObject;
  64. function deleteObject(location) {
  65. const localAPIClient = new apiv2_1.Client({ urlPrefix: api_1.storageOrigin });
  66. return localAPIClient.delete(location);
  67. }
  68. exports.deleteObject = deleteObject;
  69. async function getBucket(bucketName) {
  70. try {
  71. const localAPIClient = new apiv2_1.Client({ urlPrefix: api_1.storageOrigin });
  72. const result = await localAPIClient.get(`/storage/v1/b/${bucketName}`);
  73. return result.body;
  74. }
  75. catch (err) {
  76. logger_1.logger.debug(err);
  77. throw new error_1.FirebaseError("Failed to obtain the storage bucket", {
  78. original: err,
  79. });
  80. }
  81. }
  82. exports.getBucket = getBucket;
  83. async function listBuckets(projectId) {
  84. try {
  85. const localAPIClient = new apiv2_1.Client({ urlPrefix: api_1.storageOrigin });
  86. const result = await localAPIClient.get(`/storage/v1/b?project=${projectId}`);
  87. return result.body.items.map((bucket) => bucket.name);
  88. }
  89. catch (err) {
  90. logger_1.logger.debug(err);
  91. throw new error_1.FirebaseError("Failed to read the storage buckets", {
  92. original: err,
  93. });
  94. }
  95. }
  96. exports.listBuckets = listBuckets;
  97. async function getServiceAccount(projectId) {
  98. try {
  99. const localAPIClient = new apiv2_1.Client({ urlPrefix: api_1.storageOrigin });
  100. const response = await localAPIClient.get(`/storage/v1/projects/${projectId}/serviceAccount`);
  101. return response.body;
  102. }
  103. catch (err) {
  104. logger_1.logger.debug(err);
  105. throw new error_1.FirebaseError("Failed to obtain the Cloud Storage service agent", {
  106. original: err,
  107. });
  108. }
  109. }
  110. exports.getServiceAccount = getServiceAccount;