Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for listing bucket CID #14

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 21 additions & 2 deletions src/bucketManager.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {
CreateBucketCommand,
DeleteBucketCommand,
GetBucketAclCommand,
GetBucketAclCommand, GetBucketTaggingCommand,
ListBucketsCommand,
PutBucketAclCommand, PutBucketTaggingCommand,
S3Client,
Expand Down Expand Up @@ -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
*/

/**
Expand Down Expand Up @@ -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;
}

Expand Down
60 changes: 60 additions & 0 deletions test/bucketManager.spec.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
60 changes: 60 additions & 0 deletions test/bucketManager.spec.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Loading