Skip to content

Commit

Permalink
Remove usage of fs module because it does not work in browser
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Clark committed Aug 28, 2015
1 parent 30d5134 commit e343f3b
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 31 deletions.
3 changes: 3 additions & 0 deletions Gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ function webpackConfig(extension, overrides) {
path: './build/',
filename: ['ripple-', extension].join(pkg.version)
},
node: {
console: true
},
module: {
loaders: [{
test: /\.js$/,
Expand Down
84 changes: 63 additions & 21 deletions src/api/common/schema-validator.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
/* @flow */
// flow is disabled for this file until support for requiring json is added:
// https://github.com/facebook/flow/issues/167
'use strict';

const _ = require('lodash');
const fs = require('fs');
const path = require('path');
const assert = require('assert');
const validator = require('is-my-json-valid');
const core = require('./utils').core;
Expand All @@ -21,21 +19,66 @@ function isValidLedgerHash(ledgerHash) {
return core.UInt256.is_valid(ledgerHash);
}

function loadSchema(filepath: string): {} {
try {
return JSON.parse(fs.readFileSync(filepath, 'utf8'));
} catch (e) {
throw new Error('Failed to parse schema: ' + filepath);
}
}

function endsWith(str, suffix) {
return str.indexOf(suffix, str.length - suffix.length) !== -1;
}

function loadSchemas(dir) {
const filenames = fs.readdirSync(dir).filter(name => endsWith(name, '.json'));
const schemas = filenames.map(name => loadSchema(path.join(dir, name)));
function loadSchemas() {
// listed explicitly for webpack (instead of scanning schemas directory)
const schemas = [
require('./schemas/address.json'),
require('./schemas/adjustment.json'),
require('./schemas/amount.json'),
require('./schemas/amountbase.json'),
require('./schemas/balance.json'),
require('./schemas/blob.json'),
require('./schemas/currency.json'),
require('./schemas/get-account-info.json'),
require('./schemas/get-balances.json'),
require('./schemas/get-ledger.json'),
require('./schemas/get-orderbook.json'),
require('./schemas/get-orders.json'),
require('./schemas/get-paths.json'),
require('./schemas/get-server-info.json'),
require('./schemas/get-settings.json'),
require('./schemas/get-transaction.json'),
require('./schemas/get-transactions.json'),
require('./schemas/get-trustlines.json'),
require('./schemas/hash128.json'),
require('./schemas/hash256.json'),
require('./schemas/instructions.json'),
require('./schemas/issue.json'),
require('./schemas/ledger-options.json'),
require('./schemas/ledgerversion.json'),
require('./schemas/max-adjustment.json'),
require('./schemas/memo.json'),
require('./schemas/order-cancellation-transaction.json'),
require('./schemas/order-cancellation.json'),
require('./schemas/order-change.json'),
require('./schemas/order-transaction.json'),
require('./schemas/order.json'),
require('./schemas/orderbook-orders.json'),
require('./schemas/orderbook.json'),
require('./schemas/orders-options.json'),
require('./schemas/outcome.json'),
require('./schemas/pathfind.json'),
require('./schemas/payment-transaction.json'),
require('./schemas/payment.json'),
require('./schemas/quality.json'),
require('./schemas/remote-options.json'),
require('./schemas/sequence.json'),
require('./schemas/settings-options.json'),
require('./schemas/settings-transaction.json'),
require('./schemas/settings.json'),
require('./schemas/sign.json'),
require('./schemas/signed-value.json'),
require('./schemas/submit.json'),
require('./schemas/timestamp.json'),
require('./schemas/transaction-options.json'),
require('./schemas/transactions-options.json'),
require('./schemas/trustline-transaction.json'),
require('./schemas/trustline.json'),
require('./schemas/trustlines-options.json'),
require('./schemas/tx.json'),
require('./schemas/uint32.json'),
require('./schemas/value.json')
];
const titles = _.map(schemas, schema => schema.title);
const duplicates = _.keys(_.pick(_.countBy(titles), count => count > 1));
assert(duplicates.length === 0, 'Duplicate schemas for: ' + duplicates);
Expand Down Expand Up @@ -67,9 +110,8 @@ function schemaValidate(schemaName: string, object: any): void {
}
}

SCHEMAS = loadSchemas(path.join(__dirname, './schemas'));
SCHEMAS = loadSchemas();
module.exports = {
schemaValidate: schemaValidate,
loadSchema: loadSchema,
SCHEMAS: SCHEMAS
};
12 changes: 2 additions & 10 deletions test/api-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
'use strict';
const _ = require('lodash');
const assert = require('assert-diff');
const path = require('path');
const setupAPI = require('./setup-api');
const RippleAPI = require('ripple-api').RippleAPI;
const common = RippleAPI._PRIVATE.common;
Expand All @@ -16,6 +15,7 @@ const validate = common.validate;
const utils = RippleAPI._PRIVATE.ledgerUtils;
const ledgerClosed = require('./fixtures/api/rippled/ledger-close-newer');
const schemaValidator = RippleAPI._PRIVATE.schemaValidator;
const ledgerHashSchema = require('./fixtures/schemas/ledgerhash.json');

const orderbook = {
base: {
Expand Down Expand Up @@ -626,9 +626,7 @@ describe('RippleAPI', function() {

describe('schema-validator', function() {
beforeEach(function() {
const schema = schemaValidator.loadSchema(path.join(__dirname,
'./fixtures/schemas/ledgerhash.json'));
schemaValidator.SCHEMAS.ledgerhash = schema;
schemaValidator.SCHEMAS.ledgerhash = ledgerHashSchema;
});

it('valid', function() {
Expand All @@ -650,12 +648,6 @@ describe('RippleAPI', function() {
}, this.api.errors.ValidationError);
});

it('load schema error', function() {
assert.throws(function() {
schemaValidator.loadSchema('/bad/file/name');
}, Error);
});

it('schema not found error', function() {
assert.throws(function() {
schemaValidator.schemaValidate('unexisting', 'anything');
Expand Down

0 comments on commit e343f3b

Please sign in to comment.