Skip to content

Commit

Permalink
feat(aws-service-spec): provide a sync function to load the service s…
Browse files Browse the repository at this point in the history
…pec (#662)

In some situation we need to access the spec in a synchronous context.
This provides a convenience helper to load the spec. It's already
possible to do it, but each usage would need it's own implementation.
  • Loading branch information
mrgrain authored Nov 2, 2023
1 parent 81cb5c8 commit ffeebb0
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions packages/@aws-cdk/aws-service-spec/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,26 @@
import { promises as fs } from 'node:fs';
import { promises as fs, readFileSync } from 'node:fs';
import * as path from 'node:path';
import { gunzipSync } from 'node:zlib';
import { emptyDatabase, SpecDatabase } from '@aws-cdk/service-spec-types';

const DB_COMPRESSED = 'db.json.gz';
const DB_PATH = path.join(__dirname, '..', DB_COMPRESSED);

/**
* Load the provided built-in database
*/
export async function loadAwsServiceSpec(): Promise<SpecDatabase> {
const spec = await fs.readFile(path.join(__dirname, '..', DB_COMPRESSED));
return loadBufferIntoDatabase(await fs.readFile(DB_PATH));
}

/**
* Synchronously load the provided built-in database
*/
export function loadAwsServiceSpecSync(): SpecDatabase {
return loadBufferIntoDatabase(readFileSync(DB_PATH));
}

function loadBufferIntoDatabase(spec: Buffer): SpecDatabase {
const db = emptyDatabase();
db.load(JSON.parse(gunzipSync(spec).toString('utf-8')));
return db;
Expand Down

0 comments on commit ffeebb0

Please sign in to comment.