Skip to content

Commit

Permalink
Use json-big
Browse files Browse the repository at this point in the history
  • Loading branch information
ofrobots committed Sep 6, 2018
1 parent 4653333 commit 63e81f0
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 20 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"license": "MIT",
"dependencies": {
"axios": "^0.18.0",
"json-bigint": "^0.3.0",
"retry-axios": "0.3.2"
},
"devDependencies": {
Expand Down
9 changes: 5 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import axios from 'axios';
import * as rax from 'retry-axios';

const JSONbig = require('json-bigint');

export const HOST_ADDRESS = 'http://metadata.google.internal';
export const BASE_PATH = '/computeMetadata/v1';
export const BASE_URL = HOST_ADDRESS + BASE_PATH;
Expand Down Expand Up @@ -52,7 +54,7 @@ async function metadataAccessor<T>(
}
validate(options);
const ax = axios.create({
transformResponse: [t => t] // Do not JSON.parse strings.
transformResponse: [t => t] // Do not use default JSON.parse.
});
rax.attach(ax);
const reqOpts = {
Expand All @@ -70,10 +72,9 @@ async function metadataAccessor<T>(
} else if (!res.data) {
throw new Error('Invalid response from the metadata service');
}
if (res.headers['content-type'] === 'application/json' &&
typeof res.data === 'string') {
if (typeof res.data === 'string') {
try {
return JSON.parse(res.data);
return JSONbig.parse(res.data);
} catch {
/* ignore */
}
Expand Down
30 changes: 14 additions & 16 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,38 +49,36 @@ it('should access a specific metadata property', async () => {
scope.done();
});

it('should deal with large numbers', async () => {
it('should return large numbers as BigNumber values', async () => {
const BIG_NUMBER_STRING = `3279739563200103600`;
const scope = nock(HOST)
.get(`${PATH}/${TYPE}/${PROPERTY}`)
.reply(200, BIG_NUMBER_STRING, HEADERS);
const property = await gcp.instance(PROPERTY);
assert.strictEqual(property, BIG_NUMBER_STRING);
// property should be a BigNumber.
assert.strictEqual(property.valueOf(), BIG_NUMBER_STRING);
scope.done();
});

it('should not JSON.parse text responses', async () => {
const RESPONSE = '["Iggety, ziggety, zaggety, ZOOM!", 3279739563200103600]';
const headersWithJsonContent =
Object.assign({}, HEADERS, {'content-type': 'application/text'});
it('should return small numbers normally', async () => {
const NUMBER = 32797;
const scope = nock(HOST)
.get(`${PATH}/${TYPE}/${PROPERTY}`)
.reply(200, RESPONSE, headersWithJsonContent);
.reply(200, `${NUMBER}`, HEADERS);
const property = await gcp.instance(PROPERTY);
assert.deepStrictEqual(property, RESPONSE);
assert.strictEqual(typeof property, 'number');
assert.strictEqual(property, NUMBER);
scope.done();
});

it('should JSON.parse json responses', async () => {
const RESPONSE = '["Iggety, ziggety, zaggety, ZOOM!", 3279739563200103600]';
const headersWithJsonContent =
Object.assign({}, HEADERS, {'content-type': 'application/json'});
it('should deal with nested large numbers', async () => {
const BIG_NUMBER_STRING = `3279739563200103600`;
const RESPONSE = `{ "v1": true, "v2": ${BIG_NUMBER_STRING} }`;
const scope = nock(HOST)
.get(`${PATH}/${TYPE}/${PROPERTY}`)
.reply(200, RESPONSE, headersWithJsonContent);
const property = await gcp.instance(PROPERTY);
console.log(property);
assert.deepStrictEqual(property, JSON.parse(RESPONSE));
.reply(200, RESPONSE, HEADERS);
const response = await gcp.instance(PROPERTY);
assert.strictEqual(response.v2.valueOf(), BIG_NUMBER_STRING);
scope.done();
});

Expand Down

0 comments on commit 63e81f0

Please sign in to comment.