A comprehensive TypeScript library providing utilities and abstractions for Google Cloud Platform services including Cloud Storage, Firestore, Secret Manager, and metadata access.
npm install @farport/gcutilsRequirements:
- Node.js >=20
- Google Cloud authentication (see Authentication)
Before using any modules, ensure you have Google Cloud authentication set up:
-
Application Default Credentials (Recommended):
gcloud auth application-default login
-
Service Account Keys:
Set the
GOOGLE_APPLICATION_CREDENTIALSenvironment variable to the path of your service account key file. -
Environment Variables:
For local development, you can use environment variable fallbacks (see individual module documentation).
Powerful wrapper around Google Cloud Storage with support for gs:// style paths.
BStore- Promise-based interface for Cloud Storage operationsBStoreRx- RxJS Observable-based interface for reactive programming- Features: Automatic path parsing, streaming support, metadata handling
Class-driven interface for Google Cloud Firestore with built-in validation and type safety.
FirestoreService- Promise-based Firestore operationsAbstractBaseFirebaseModel- Base class for type-safe document models- Features: Automatic validation, namespace support, query capabilities, multi-tenant applications
Service-oriented interface for Google Cloud Secret Manager with caching and version management.
SecretsService- Promise-based secret management with async factory pattern- Features: In-memory caching, version management, bulk secret loading, error handling with defaults
Service-oriented interface for Google Cloud Pub/Sub with built-in error handling and JSON message support.
PubSubService- Main service class for managing topics and subscriptionsPublisher- Dedicated class for publishing messages with JSON supportSubscriber- Dedicated class for consuming messages with automatic error handling- Features: Automatic topic/subscription creation, JSON message support, retry logic, clean resource management
Easy access to Google Cloud Metadata server for instance and project information.
GCloudMetadata- Cached access to Google Cloud metadata- Features: Automatic caching, environment variable fallbacks, type-safe property access
// Cloud Storage
import { BStore, BStoreRx } from '@farport/gcutils/cloud-storage';
const store = new BStore();
// Firestore
import { FirestoreService } from '@farport/gcutils/firestore';
const firestore = new FirestoreService({ projectId: 'your-project' });
// Pub/Sub
import { PubSubService } from '@farport/gcutils/pubsub';
const pubsub = new PubSubService({ projectId: 'your-project' });
// Secret Manager
import { SecretsService } from '@farport/gcutils/secret-manager';
const secrets = await SecretsService.create({ projectId: 'your-project' });
// Metadata
import { GCloudMetadata } from '@farport/gcutils/metadata';
const metadata = new GCloudMetadata();Prerequisites:
- Make sure that a google cloud project is activated using gcloud
- You must create a
.env.localwith required test env variables - Ensure that files in the
test/datais in a bucketgs://<GCUTILS_TEST_BUCKET>/public/directory - Start emulators by running
yarn emulators:up
Content of .env.local:
GCUTILS_GOOGLE_CLOUD_PROJECT="<Google Project Name>"
GCUTILS_GOOGLE_CLOUD_PROJECT_NUMBER="<Google Project Number>"
GCUTILS_TEST_BUCKET="<Google Cloud Storage Bucket Name>"
GCUTILS_METADATA_PROXY_URL="<Secure Cloud Run URL that proxy to the metadata.google.internal host>"You can easily skip the test/metadata/mdata-proxy.spec.ts test if you do not have a GCUTILS_METADATA_PROXY_URL
Run Tests:
# Run all tests with coverage
yarn test
# Run tests that exits upon first failure
yarn utest
# Run library tests
yarn test:libStop Emulators:
yarn emulators:downBuild the library:
yarn buildLint and format:
yarn lintGenerate documentation:
yarn gendocClean build artifacts:
yarn clean