schema-client
Advanced tools
Comparing version 1.2.9 to 1.2.10
@@ -9,13 +9,9 @@ /** | ||
var Connection = require('./connection').Connection; | ||
var Schema = require('./schema'); | ||
var Collection = Schema.Collection; | ||
var Record = Schema.Record; | ||
Schema.Connection = require('./connection').Connection; | ||
var defaults = { | ||
host: 'api.schema.io', | ||
port: 8443, | ||
verifyCert: true, | ||
version: 1 | ||
}; | ||
var DEFAULT_HOST = 'api.schema.io'; | ||
var DEFAULT_PORT = 8443; | ||
var DEFAULT_VERIFY_CERT = true; | ||
var DEFAULT_VERSION = 1; | ||
@@ -36,4 +32,5 @@ /** | ||
this.init(clientId, clientKey, options, callback); | ||
if (clientId) { | ||
this.init(clientId, clientKey, options); | ||
} | ||
if (callback) { | ||
@@ -52,3 +49,2 @@ this.connect(callback); | ||
* @param object options | ||
* @param function callback | ||
*/ | ||
@@ -59,9 +55,2 @@ Client.prototype.init = function(clientId, clientKey, options) { | ||
if (typeof options === 'function') { | ||
callback = options; | ||
options = {}; | ||
} else if (typeof clientKey === 'function') { | ||
callback = clientKey; | ||
options = {}; | ||
} | ||
if (typeof clientKey === 'object') { | ||
@@ -76,12 +65,18 @@ options = clientKey; | ||
this.params = { | ||
host: options.host || defaults.host, | ||
port: options.port || defaults.port, | ||
clientId: clientId || options.id, | ||
clientKey: clientKey || options.key, | ||
clear: options.clear !== undefined ? options.clear : defaults.clear, | ||
verifyCert: options.verifyCert !== undefined ? options.verifyCert : defaults.verifyCert, | ||
version: options.version || defaults.version, | ||
session: options.session, | ||
api: options.api | ||
host: options.host || DEFAULT_HOST, | ||
port: options.port || DEFAULT_PORT, | ||
clear: options.clear !== undefined ? options.clear : false, // only for local proxy | ||
verifyCert: options.verifyCert !== undefined ? options.verifyCert : DEFAULT_VERIFY_CERT, | ||
version: options.version || DEFAULT_VERSION, | ||
session: options.session | ||
}; | ||
if (!this.params.clientId) { | ||
throw 'Schema client `id` is required to initialize'; | ||
} | ||
if (!this.params.clientKey) { | ||
throw 'Schema client `key` is required to initialize'; | ||
} | ||
}; | ||
@@ -97,3 +92,3 @@ | ||
var self = this; | ||
this.server = new Connection( | ||
this.server = new Schema.Connection( | ||
this.params.host, | ||
@@ -110,3 +105,6 @@ this.params.port, | ||
); | ||
this.server.on('error', function(err, type) { | ||
this.server.on('close', function() { | ||
self.emit('close'); | ||
}); | ||
this.server.on('error', function(err) { | ||
self.emit('error', 'Error: '+err); | ||
@@ -123,10 +121,2 @@ }); | ||
}); | ||
// Fallback handler | ||
this.on('error', function(err) { | ||
var lcount = events.EventEmitter.listenerCount(self, 'error'); | ||
if (lcount === 1) { | ||
throw 'Schema Client: '+err; | ||
} | ||
}); | ||
}; | ||
@@ -170,3 +160,3 @@ | ||
if (result.$end) { | ||
// Connection ended, retry | ||
// Connection ended, retry auth | ||
return self.request(method, url, data.$data, callback); | ||
@@ -202,7 +192,3 @@ } else { | ||
&& (typeof result.$data === 'object')) { | ||
// TODO: use a header to determine url of a new record | ||
if (method.toLowerCase() === 'post') { | ||
url = url.replace(/\/$/, '') + '/' + result.$data.id; | ||
} | ||
actualResult = Client.createResource(url, result, this); | ||
actualResult = Client.createResource(result.$url, result, this); | ||
} else { | ||
@@ -291,5 +277,2 @@ actualResult = result.$data; | ||
}; | ||
if (this.params.api) { | ||
creds.$api = this.params.api; | ||
} | ||
if (this.params.version) { | ||
@@ -314,3 +297,5 @@ creds.$v = this.params.version; | ||
this.server.close(); | ||
if (this.server) { | ||
this.server.close(); | ||
} | ||
}; | ||
@@ -341,5 +326,5 @@ | ||
if (result && result.$data && 'count' in result.$data && result.$data.results) { | ||
return new Collection(url, result, client); | ||
return new Schema.Collection(url, result, client); | ||
} | ||
return new Record(url, result, client); | ||
return new Schema.Record(url, result, client); | ||
}; | ||
@@ -350,4 +335,2 @@ | ||
module.exports.Client = Client; | ||
module.exports.Connection = Connection; | ||
module.exports.defaults = defaults; | ||
module.exports.createClient = Client.create; |
@@ -5,3 +5,2 @@ /** | ||
var crypto = require('crypto'); | ||
var inherits = require('util').inherits; | ||
@@ -12,2 +11,4 @@ var events = require('events'); | ||
var DEFAULT_NETWORK_ERROR = 'Server unexpectedly closed network connection.'; | ||
/** | ||
@@ -22,3 +23,3 @@ * Connection constructor | ||
var Connection = function(host, port, options, callback) { | ||
events.EventEmitter.call(this); | ||
@@ -29,3 +30,2 @@ | ||
this.buffer = []; | ||
this.responders = []; | ||
this.requestBuffer = []; | ||
@@ -64,3 +64,3 @@ | ||
rejectUnauthorized: this.options.verifyCert === false ? false : true | ||
}, function(stream) { | ||
}, function() { | ||
self.connected = true; | ||
@@ -71,3 +71,3 @@ self.flushRequestBuffer(); | ||
}); | ||
this.stream.on('error', this.emit.bind(this, 'error.network')); | ||
this.stream.on('error', this.close.bind(this)); | ||
this.stream.on('data', this.receive.bind(this)); | ||
@@ -87,3 +87,3 @@ this.stream.on('close', this.close.bind(this)); | ||
*/ | ||
Connection.prototype.request = function(method) { | ||
Connection.prototype.request = function() { | ||
@@ -96,4 +96,5 @@ // Copy args to avoid leaking | ||
this.requestBuffer.push(args); | ||
if (!this.connected) { | ||
this.requestBuffer.push(args); | ||
if (!this.stream) { | ||
@@ -105,10 +106,3 @@ this.connect(); | ||
if (!this.stream) { | ||
this.emit('error.network', 'Unable to execute '+method+' (Error: Connection closed)'); | ||
return; | ||
} | ||
var response = args.pop(); | ||
var request = JSON.stringify(args); | ||
this.responders.push(response); | ||
var request = JSON.stringify(args.slice(0, -1)); | ||
this.stream.write(request + "\n"); | ||
@@ -150,2 +144,3 @@ }; | ||
var response; | ||
var responder = this.requestBuffer.shift().pop(); | ||
@@ -155,9 +150,17 @@ try { | ||
} catch (err) { | ||
this.emit('error.protocol', 'Unable to parse response from server ('+data+')'); | ||
return; | ||
response = 'Unable to parse response from server ('+data+')'; | ||
this.emit('error.protocol', response); | ||
return responder({ | ||
$status: 500, | ||
$error: response | ||
}); | ||
} | ||
if (!response || typeof response !== 'object') { | ||
this.emit('error.protocol', 'Invalid response from server ('+data+')'); | ||
return; | ||
response = 'Invalid response from server ('+data+')'; | ||
this.emit('error.protocol', response); | ||
return responder({ | ||
$status: 500, | ||
$error: response | ||
}); | ||
} | ||
@@ -173,4 +176,3 @@ | ||
// Note: response always returns in the same order as request | ||
if (typeof this.responders[0] === 'function') { | ||
var responder = this.responders.shift(); | ||
if (typeof responder === 'function') { | ||
responder(response); | ||
@@ -183,11 +185,29 @@ } | ||
*/ | ||
Connection.prototype.close = function() { | ||
Connection.prototype.close = function(error) { | ||
if (this.stream) { | ||
if (!this.connected) { | ||
return; | ||
} | ||
if (this.stream && this.stream.writable) { | ||
this.stream.end(); | ||
} | ||
this.connected = false; | ||
this.disconnected = true; | ||
this.stream = null; | ||
this.emit('close'); | ||
var hasRequests = this.requestBuffer.length; | ||
if (error || hasRequests) { | ||
this.emit('error.network', error || DEFAULT_NETWORK_ERROR); | ||
} | ||
var responder; | ||
while (--hasRequests >= 0) { | ||
responder = this.requestBuffer.shift().pop(); | ||
responder({ | ||
$status: 500, | ||
$error: error || DEFAULT_NETWORK_ERROR | ||
}); | ||
} | ||
}; | ||
@@ -202,8 +222,10 @@ | ||
if (this.requestBuffer.length && this.connected) { | ||
for (var i = 0; i < this.requestBuffer.length; i++) { | ||
this.request.apply(this, this.requestBuffer[i]); | ||
} | ||
if (!this.connected) { | ||
return; | ||
} | ||
this.requestBuffer = []; | ||
var hasRequests = this.requestBuffer.length; | ||
while (--hasRequests >= 0) { | ||
this.request.apply(this, this.requestBuffer.shift()); | ||
} | ||
}; | ||
@@ -210,0 +232,0 @@ |
{ | ||
"name": "schema-client", | ||
"version": "1.2.9", | ||
"description": "Schema API Client for NodeJS", | ||
"author": "Schema", | ||
"main": "./lib/client.js", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/schemaio/schema-node-client.git" | ||
} | ||
"name": "schema-client", | ||
"version": "1.2.10", | ||
"description": "Schema API Client for NodeJS", | ||
"keywords": [ | ||
"schema", | ||
"ecommerce", | ||
"payments", | ||
"api" | ||
], | ||
"homepage": "https://github.com/schemaio/schema-node-client", | ||
"author": "Schema", | ||
"repository": { | ||
"type": "git", | ||
"url": "git://github.com/schemaio/schema-node-client.git" | ||
}, | ||
"bugs:": "https://github.com/schemaio/schema-node-client/issues", | ||
"engines": { | ||
"node": ">= v0.10.0" | ||
}, | ||
"main": "./lib/client.js", | ||
"devDependencies": { | ||
"chai": "~3.4.1", | ||
"eslint": "^1.10.3", | ||
"mocha": "~2.3.4", | ||
"sinon": "^1.17.2" | ||
}, | ||
"dependencies": { | ||
"bluebird": "^3.1.1" | ||
}, | ||
"license": "MIT", | ||
"scripts": { | ||
"test": "mocha" | ||
} | ||
} |
@@ -1,2 +0,2 @@ | ||
## Schema API Client for NodeJS | ||
## Schema API Client for NodeJS [![Build Status](https://travis-ci.org/schemaio/schema-node-client.png?branch=master)](https://travis-ci.org/schemaio/schema-node-client) | ||
@@ -3,0 +3,0 @@ Build and scale ecommerce with Schema. Create a free account at <https://schema.io> |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
No tests
QualityPackage does not have any tests. This is a strong signal of a poorly maintained or low quality package.
Found 1 instance in 1 package
58000
11
851
1
2
1
4
+ Addedbluebird@^3.1.1
+ Addedbluebird@3.7.2(transitive)