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

Fix CJS support #6

Merged
merged 6 commits into from
Feb 8, 2024
Merged
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
20 changes: 12 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,28 +1,32 @@
{
"name": "@filebase/sdk",
"version": "1.0.2",
"version": "1.0.3",
"description": "SDK for Interacting with Filebase Services [S3(Buckets, Objects), IPFS(Gateways, Pins) IPNS(Names)]",
"repository": {
"type": "git",
"url": "git+https://github.com/filebase/filebase-sdk.git"
},
"license": "MIT",
"type": "module",
"main": "./dist/index.js",
"module": "./src/index.js",
"types": "./dist/index.d.ts",
"exports": {
".": {
"import": "./dist/index.mjs",
"require": "./dist/index.cjs"
"require": "./dist/index.js",
"import": "./src/index.js",
"types": "./dist/index.d.ts"
}
},
"files": [
"dist"
"dist",
"src"
],
"engines": {
"node": ">=16.0.0",
"npm": ">=8.0.0"
},
"scripts": {
"build": "unbuild",
"build": "tsup src/index.js --format cjs --dts --clean",
"test": "node --test",
"doc": "jsdoc -c jsdoc.json"
},
Expand All @@ -40,8 +44,8 @@
"clean-jsdoc-theme": "4.2.17",
"jsdoc": "4.0.2",
"prettier": "3.1.0",
"typescript": "5.3.3",
"unbuild": "2.0.0"
"tsup": "8.0.1",
"typescript": "5.3.3"
},
"dependencies": {
"@aws-sdk/client-s3": "3.478.0",
Expand Down
3 changes: 3 additions & 0 deletions src/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"type": "module"
}
124 changes: 124 additions & 0 deletions test/bucketManager.spec.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
const test = require("node:test");
const assert = require("node:assert/strict");
const { BucketManager } = require("../dist/index.js");

const TEST_PREFIX = Date.now();

test("create bucket", 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 bucketNameToCreate = `${TEST_PREFIX}-create-bucket-test-pass`;
await bucketManager.create(bucketNameToCreate);

try {
// List buckets
const currentBuckets = await bucketManager.list(),
createdBucket = currentBuckets.find((currentBucket) => {
return currentBucket.Name === bucketNameToCreate;
});

// Assert new bucket exists
assert.equal(createdBucket.Name, bucketNameToCreate);
} finally {
// Delete new bucket
await bucketManager.delete(bucketNameToCreate);
}
});

test("list buckets", async () => {
const testBucketName = `${TEST_PREFIX}-list-bucket-test-pass`,
bucketManager = new BucketManager(
process.env.TEST_S3_KEY || process.env.TEST_KEY,
process.env.TEST_S3_SECRET || process.env.TEST_SECRET,
),
initialBucketsList = await bucketManager.list(),
countToCreate = 3;
for (let i = 0; i < countToCreate; i++) {
await bucketManager.create(`${testBucketName}-${i}`);
}
const bucketsList = await bucketManager.list();
for (let i = 0; i < countToCreate; i++) {
await bucketManager.delete(`${testBucketName}-${i}`);
}
assert.strictEqual(
bucketsList.length,
initialBucketsList.length + countToCreate,
);
});

test("delete bucket", 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 `delete-bucket-test-pass`
const bucketNameToCreate = `${TEST_PREFIX}-delete-bucket-test-pass`;
await bucketManager.create(bucketNameToCreate);

// List buckets and assert new bucket exists
const currentBuckets = await bucketManager.list(),
createdBucket = currentBuckets.find((currentBucket) => {
return currentBucket.Name === bucketNameToCreate;
});
if (typeof createdBucket === "undefined") {
throw new Error(`Unable to create test bucket [delete-bucket-test-pass]`);
}

// Delete new bucket
await bucketManager.delete(bucketNameToCreate);

// List buckets and assert new bucket does not exist
const updatedBuckets = await bucketManager.list(),
deletedBucket = updatedBuckets.find((updatedBucket) => {
return updatedBucket.Name === bucketNameToCreate;
});
assert.equal(typeof deletedBucket, "undefined");
});

test("set bucket privacy to public", 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 `toggle-bucket-test-pass`
const bucketNameToCreate = `${TEST_PREFIX}-toggle-bucket-test-pass`;
await bucketManager.create(bucketNameToCreate);

try {
// List buckets
const currentBuckets = await bucketManager.list(),
createdBucket = currentBuckets.find((currentBucket) => {
return currentBucket.Name === bucketNameToCreate;
});

// Check Privacy
const initialPrivacy = await bucketManager.getPrivacy(bucketNameToCreate);
if (initialPrivacy === false) {
throw new Error(`Unexpected Privacy State on Bucket`);
}

// Toggle Privacy
await bucketManager.setPrivacy(bucketNameToCreate, false);

// Check Privacy
const updatedPrivacy = await bucketManager.getPrivacy(bucketNameToCreate);
if (updatedPrivacy === true) {
throw new Error(`Unexpected Privacy State on Bucket`);
}

// Assert new bucket exists
assert.equal(createdBucket.Name, bucketNameToCreate);
} finally {
// Delete new bucket
await bucketManager.delete(bucketNameToCreate);
}
});
8 changes: 4 additions & 4 deletions test/bucketManager.spec.js → test/bucketManager.spec.mjs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import test from "node:test";
import assert from "node:assert/strict";
import { BucketManager } from "../dist/index.mjs";
import { BucketManager } from "../src/index.js";

const TEST_PREFIX = Date.now();

test("create bucket", async (t) => {
test("create bucket", async () => {
// Initialize BucketManager
const bucketManager = new BucketManager(
process.env.TEST_S3_KEY || process.env.TEST_KEY,
Expand Down Expand Up @@ -51,7 +51,7 @@ test("list buckets", async () => {
);
});

test("delete bucket", async (t) => {
test("delete bucket", async () => {
// Initialize BucketManager
const bucketManager = new BucketManager(
process.env.TEST_S3_KEY || process.env.TEST_KEY,
Expand Down Expand Up @@ -82,7 +82,7 @@ test("delete bucket", async (t) => {
assert.equal(typeof deletedBucket, "undefined");
});

test("set bucket privacy to public", async (t) => {
test("set bucket privacy to public", async () => {
// Initialize BucketManager
const bucketManager = new BucketManager(
process.env.TEST_S3_KEY || process.env.TEST_KEY,
Expand Down
125 changes: 125 additions & 0 deletions test/gatewayManager.spec.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
const test = require("node:test");
const assert = require("node:assert/strict");
const { GatewayManager } = require("../dist/index.js");

const TEST_PREFIX = Date.now();

test("delete gateway", async () => {
const testGatewayName = `${TEST_PREFIX}-delete-gateway-test-pass`,
gatewayManager = new GatewayManager(
process.env.TEST_GW_KEY || process.env.TEST_KEY,
process.env.TEST_GW_SECRET || process.env.TEST_SECRET,
);
await gatewayManager.create(testGatewayName);
await gatewayManager.delete(testGatewayName);
const deletedName = await gatewayManager.get(testGatewayName);
assert.strictEqual(deletedName, false);
});
test("create gateway", async () => {
const testGatewayName = `${TEST_PREFIX}-create-gateway-test-pass`,
gatewayManager = new GatewayManager(
process.env.TEST_GW_KEY || process.env.TEST_KEY,
process.env.TEST_GW_SECRET || process.env.TEST_SECRET,
),
createdName = await gatewayManager.create(testGatewayName);
await gatewayManager.delete(testGatewayName);
assert.strictEqual(createdName.name, testGatewayName);
});

test("update gateway", async () => {
const testGatewayName = `${TEST_PREFIX}-update-gateway-test-pass`,
gatewayManager = new GatewayManager(
process.env.TEST_GW_KEY || process.env.TEST_KEY,
process.env.TEST_GW_SECRET || process.env.TEST_SECRET,
),
createdName = await gatewayManager.create(testGatewayName);
try {
const updatedName = await gatewayManager.update(createdName.name, {
private: true,
enabled: false,
});
assert.strictEqual(updatedName, true);
} finally {
await gatewayManager.delete(testGatewayName);
}
});

test("get gateway", async () => {
const testGatewayName = `${TEST_PREFIX}-get-gateway-test-pass`,
gatewayManager = new GatewayManager(
process.env.TEST_GW_KEY || process.env.TEST_KEY,
process.env.TEST_GW_SECRET || process.env.TEST_SECRET,
),
createdName = await gatewayManager.create(testGatewayName, {});
try {
const testName = await gatewayManager.get(createdName.name);
assert.strictEqual(testName.name, testGatewayName);
} finally {
await gatewayManager.delete(testGatewayName);
}
});

test("list gateways", async () => {
const testGatewayName = `${TEST_PREFIX}-list-names-test-pass`,
gatewayManager = new GatewayManager(
process.env.TEST_GW_KEY || process.env.TEST_KEY,
process.env.TEST_GW_SECRET || process.env.TEST_SECRET,
),
initialGatewaysList = await gatewayManager.list(),
countToCreate = 3;
for (let i = 0; i < countToCreate; i++) {
await gatewayManager.create(`${testGatewayName}-${i}`);
}
const gatewaysList = await gatewayManager.list();
for (let i = 0; i < countToCreate; i++) {
await gatewayManager.delete(`${testGatewayName}-${i}`);
}
assert.strictEqual(
gatewaysList.length,
initialGatewaysList.length + countToCreate,
);
});

test("toggle gateway off", async () => {
const testGatewayName = `${TEST_PREFIX}-toggle-gateway-test-pass`,
gatewayManager = new GatewayManager(
process.env.TEST_GW_KEY || process.env.TEST_KEY,
process.env.TEST_GW_SECRET || process.env.TEST_SECRET,
);
await gatewayManager.create(testGatewayName);
try {
const resolvedName = await gatewayManager.get(testGatewayName);
if (resolvedName?.enabled === false) {
throw new Error(`Incorrect State on Resolved Name`);
}
await gatewayManager.toggle(testGatewayName, false);
const updatedName = await gatewayManager.get(testGatewayName);
assert.strictEqual(updatedName.name, testGatewayName);
assert.strictEqual(updatedName.enabled, false);
} finally {
await gatewayManager.delete(testGatewayName);
}
});

test("toggle gateway on", async () => {
const testGatewayName = `${TEST_PREFIX}-toggle-gateway-test-pass`,
gatewayManager = new GatewayManager(
process.env.TEST_GW_KEY || process.env.TEST_KEY,
process.env.TEST_GW_SECRET || process.env.TEST_SECRET,
);
await gatewayManager.create(testGatewayName, {
enabled: false,
});
try {
const resolvedName = await gatewayManager.get(testGatewayName);
if (resolvedName?.enabled === true) {
throw new Error(`Incorrect State on Resolved Name`);
}
await gatewayManager.toggle(testGatewayName, true);
const updatedName = await gatewayManager.get(testGatewayName);
assert.strictEqual(updatedName.name, testGatewayName);
assert.strictEqual(updatedName.enabled, true);
} finally {
await gatewayManager.delete(testGatewayName);
}
});
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { test } from "node:test";
import assert from "node:assert/strict";
import { GatewayManager } from "../dist/index.mjs";
import { GatewayManager } from "../src/index.js";

const TEST_PREFIX = Date.now();

Expand Down
Loading
Loading