diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml index 2ceb9b1..e177bcd 100644 --- a/.github/workflows/publish.yml +++ b/.github/workflows/publish.yml @@ -11,7 +11,7 @@ jobs: - name: Setup Node uses: actions/setup-node@v4 with: - node-version: '20.x' + node-version: '22.x' registry-url: 'https://registry.npmjs.org' - run: yarn install --frozen-lockfile - run: yarn run build diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index f977e9c..df4e06f 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -11,7 +11,7 @@ jobs: - name: Use Node.js uses: actions/setup-node@v3 with: - node-version: '20.x' + node-version: '22.x' - run: yarn install --frozen-lockfile - run: yarn run build - run: node --test --test-concurrency 1 diff --git a/src/bucketManager.js b/src/bucketManager.js index f09c1ac..54af04c 100644 --- a/src/bucketManager.js +++ b/src/bucketManager.js @@ -1,7 +1,7 @@ import { CreateBucketCommand, DeleteBucketCommand, - GetBucketAclCommand, + GetBucketAclCommand, GetBucketTaggingCommand, ListBucketsCommand, PutBucketAclCommand, PutBucketTaggingCommand, S3Client, @@ -43,7 +43,8 @@ class BucketManager { /** * @typedef {Object} bucket * @property {string} Name The name of the bucket - * @property {date} Date the bucket was created + * @property {date} Date Date the bucket was created + * @property {function} CID Function to retrieve current CID of bucket */ /** @@ -73,6 +74,24 @@ class BucketManager { const command = new ListBucketsCommand({}), { Buckets } = await this.#client.send(command); + for (const bucket of Buckets) { + bucket.CID = async () => { + const getCidCommand = new GetBucketTaggingCommand({ + Bucket: bucket.Name, + }); + const getCidResponse = await this.#client.send(getCidCommand) + if (typeof getCidResponse !== "undefined" && getCidResponse.TagSet !== "undefined") { + const resolvedTag = getCidResponse.TagSet.find((element) => { + return element.Key === "CID" + }); + if (typeof resolvedTag !== "undefined" && resolvedTag.Value !== "") { + return resolvedTag.Value; + } + } + return undefined; + } + } + return Buckets; } diff --git a/test/bucketManager.spec.cjs b/test/bucketManager.spec.cjs index 8d97455..3bc0dfa 100644 --- a/test/bucketManager.spec.cjs +++ b/test/bucketManager.spec.cjs @@ -53,6 +53,66 @@ test("generate bucket cid", async () => { } }); +test("list bucket cid", async () => { + // Initialize BucketManager + const bucketManager = new BucketManager( + process.env.TEST_S3_KEY || process.env.TEST_KEY, + process.env.TEST_S3_SECRET || process.env.TEST_SECRET, + ); + + // Create bucket `create-bucket-test-pass` + const bucketNameToGenerate = `${TEST_PREFIX}-list-bucket-cid-test-pass`; + await bucketManager.create(bucketNameToGenerate); + + try { + // Generate bucket CID + const generatedCid = await bucketManager.generateCid(bucketNameToGenerate); + + // Assert new bucket exists + assert.equal(generatedCid, "bafybeiczsscdsbs7ffqz55asqdf3smv6klcw3gofszvwlyarci47bgf354"); + + // List buckets + const bucketsList = await bucketManager.list(), + listedBucket = bucketsList.find((element) => { + return element.Name === bucketNameToGenerate; + }), + listedBucketCid = await listedBucket.CID() + + // Assert listed CID + assert.equal(listedBucketCid, "bafybeiczsscdsbs7ffqz55asqdf3smv6klcw3gofszvwlyarci47bgf354"); + } finally { + // Delete new bucket + await bucketManager.delete(bucketNameToGenerate); + } +}); + +test("list bucket without cid", async () => { + // Initialize BucketManager + const bucketManager = new BucketManager( + process.env.TEST_S3_KEY || process.env.TEST_KEY, + process.env.TEST_S3_SECRET || process.env.TEST_SECRET, + ); + + // Create bucket `create-bucket-test-pass` + const bucketNameToGenerate = `${TEST_PREFIX}-list-bucket-without-cid-test-pass`; + await bucketManager.create(bucketNameToGenerate); + + try { + // List buckets + const bucketsList = await bucketManager.list(), + listedBucket = bucketsList.find((element) => { + return element.Name === bucketNameToGenerate; + }), + listedBucketCid = await listedBucket.CID() + + // Assert listed CID + assert.equal(listedBucketCid, undefined); + } finally { + // Delete new bucket + await bucketManager.delete(bucketNameToGenerate); + } +}); + test("list buckets", async () => { const testBucketName = `${TEST_PREFIX}-list-bucket-test-pass`, bucketManager = new BucketManager( diff --git a/test/bucketManager.spec.mjs b/test/bucketManager.spec.mjs index 5ed3f83..8f81950 100644 --- a/test/bucketManager.spec.mjs +++ b/test/bucketManager.spec.mjs @@ -53,6 +53,66 @@ test("generate bucket cid", async () => { } }); +test("list bucket cid", async () => { + // Initialize BucketManager + const bucketManager = new BucketManager( + process.env.TEST_S3_KEY || process.env.TEST_KEY, + process.env.TEST_S3_SECRET || process.env.TEST_SECRET, + ); + + // Create bucket `create-bucket-test-pass` + const bucketNameToGenerate = `${TEST_PREFIX}-list-bucket-cid-test-pass`; + await bucketManager.create(bucketNameToGenerate); + + try { + // Generate bucket CID + const generatedCid = await bucketManager.generateCid(bucketNameToGenerate); + + // Assert new bucket exists + assert.equal(generatedCid, "bafybeiczsscdsbs7ffqz55asqdf3smv6klcw3gofszvwlyarci47bgf354"); + + // List buckets + const bucketsList = await bucketManager.list(), + listedBucket = bucketsList.find((element) => { + return element.Name === bucketNameToGenerate; + }), + listedBucketCid = await listedBucket.CID() + + // Assert listed CID + assert.equal(listedBucketCid, "bafybeiczsscdsbs7ffqz55asqdf3smv6klcw3gofszvwlyarci47bgf354"); + } finally { + // Delete new bucket + await bucketManager.delete(bucketNameToGenerate); + } +}); + +test("list bucket without cid", async () => { + // Initialize BucketManager + const bucketManager = new BucketManager( + process.env.TEST_S3_KEY || process.env.TEST_KEY, + process.env.TEST_S3_SECRET || process.env.TEST_SECRET, + ); + + // Create bucket `create-bucket-test-pass` + const bucketNameToGenerate = `${TEST_PREFIX}-list-bucket-without-cid-test-pass`; + await bucketManager.create(bucketNameToGenerate); + + try { + // List buckets + const bucketsList = await bucketManager.list(), + listedBucket = bucketsList.find((element) => { + return element.Name === bucketNameToGenerate; + }), + listedBucketCid = await listedBucket.CID() + + // Assert listed CID + assert.equal(listedBucketCid, undefined); + } finally { + // Delete new bucket + await bucketManager.delete(bucketNameToGenerate); + } +}); + test("list buckets", async () => { const testBucketName = `${TEST_PREFIX}-list-bucket-test-pass`, bucketManager = new BucketManager(