Skip to content
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
5 changes: 2 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
on:
push:
branches: [main]
pull_request:
name: ci
jobs:
Expand All @@ -11,7 +10,7 @@ jobs:
node: [18, 20]
include:
# use latest npm by default

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: remove comment

- npm-version: latest
- npm-version: 10.9.2
steps:
- uses: actions/checkout@c85c95e3d7251135ab7dc9ce3241c5835cc595a9 # v3.5.3
- uses: actions/setup-node@64ed1c7eab4cce3362f8c340dee64e5eaeef8f7c # v3.6.0
Expand All @@ -21,4 +20,4 @@ jobs:
- name: Upgrade to latest npm to support lockfile v2
run: npm install -g npm@${{ matrix.npm-version }}
- run: npm install && npm install
- run: npm run test
- run: npm run test
6 changes: 2 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions packages/synthetics-sdk-api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@
"error-stack-parser": "2.1.4",
"google-auth-library": "9.0.0",
"ts-proto": "1.148.1",
"winston": "3.10.0",
"axios": "1.6.7"
"winston": "3.10.0"
},
"author": "Google Inc.",
"license": "Apache-2.0"
Expand Down
27 changes: 14 additions & 13 deletions packages/synthetics-sdk-api/src/cloud_region_resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

import axios, { AxiosResponse } from 'axios';

/**
*
* @public
*
* Retrieves the region in which the current Google Cloud Function (v2) is
Expand All @@ -24,24 +21,28 @@ import axios, { AxiosResponse } from 'axios';
export async function getExecutionRegion(): Promise<string | null> {
const metadataServerUrl =
'http://metadata.google.internal/computeMetadata/v1/instance/region';
const headers = { 'Metadata-Flavor': 'Google' };
const options = {
headers: {
'Metadata-Flavor': 'Google',
},
};

try {
const response: AxiosResponse = await axios.get(metadataServerUrl, {
headers,
});
const response = await fetch(metadataServerUrl, options);

if (!response.ok) {
throw new Error(`Request failed with status ${response.status}`);
}

const body = await response.text();

// Extract region from the response (e.g., 'us-east1')
const regions = response.data.split('/');
const regions = body.split('/');
const region = regions[regions.length - 1];

return region;
} catch (error) {
if (axios.isAxiosError(error)) {
console.error('Error fetching region from metadata server:', error);
} else {
console.error('Unexpected error:', error);
}
console.error('Error fetching region from metadata server:', error);
}

return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,34 +12,38 @@
// See the License for the specific language governing permissions and
// limitations under the License.

import axios from 'axios';
import * as sinon from 'sinon';
import { expect } from 'chai';
import { getExecutionRegion } from '../../src/cloud_region_resolver';

describe('getExecutionRegion', () => {
let axiosGetStub: sinon.SinonStub;
describe('getExecutionRegion (with fetch)', () => {
let fetchStub: sinon.SinonStub;

beforeEach(() => {
axiosGetStub = sinon.stub(axios, 'get');
fetchStub = sinon.stub(global, 'fetch');
});

afterEach(() => {
axiosGetStub.restore();
fetchStub.restore();
});

it('should retrieve the region from the metadata server', async () => {
axiosGetStub.resolves({ data: 'projects/123456789/regions/us-east1' });
const mockResponse = {
ok: true,
text: () => Promise.resolve('projects/123456789/regions/us-east1'),
};
fetchStub.resolves(mockResponse as any);

const region = await getExecutionRegion();
expect(region).to.equal('us-east1');
expect(axiosGetStub.calledOnce).to.be.true;
expect(fetchStub.calledOnce).to.be.true;
});

it('should handle errors from the metadata server', async () => {
axiosGetStub.rejects(new Error('Metadata server error'));
fetchStub.rejects(new Error('Network error'));

const region = await getExecutionRegion();

expect(region).to.be.null;
});
});
3 changes: 2 additions & 1 deletion packages/synthetics-sdk-api/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"composite": true,
"resolveJsonModule": true,
"esModuleInterop": true,
"module": "commonjs"
"module": "commonjs",
"lib": ["es2020", "dom"]
},
"include": [
"src/**/*",
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.base.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"extends": "./node_modules/gts/tsconfig-google.json"
}
}