@4c/graphql-subscription-server
Advanced tools
Comparing version 0.0.3 to 0.1.0
function _asyncIterator(iterable) { if (typeof Symbol === "function") { if (Symbol.asyncIterator) { var method = iterable[Symbol.asyncIterator]; if (method != null) return method.call(iterable); } if (Symbol.iterator) { return iterable[Symbol.iterator](); } } throw new TypeError("Object is not async iterable"); } | ||
import { parse, subscribe } from 'graphql'; | ||
import { parse, subscribe, validate, specifiedRules } from 'graphql'; | ||
import IoServer from 'socket.io'; | ||
@@ -26,3 +26,3 @@ import * as AsyncUtils from './AsyncUtils'; | ||
this.log('error', err.message, err); | ||
this.config.socket.emit('app_error', { | ||
this.emitError({ | ||
code: 'invalid_authorization' | ||
@@ -40,14 +40,2 @@ }); | ||
}, cb) => { | ||
if (this.subscriptions.has(id)) { | ||
this.log('debug', 'Duplicate subscription attempted', { | ||
id | ||
}); | ||
this.config.socket.emit('app_error', { | ||
code: 'invalid_id.duplicate', | ||
detail: id | ||
}); | ||
acknowledge(cb); | ||
return; | ||
} | ||
if (this.config.maxSubscriptionsPerConnection != null && this.subscriptions.size >= this.config.maxSubscriptionsPerConnection) { | ||
@@ -57,3 +45,3 @@ this.log('error', 'Max Subscription limit reached', { | ||
}); | ||
this.config.socket.emit('app_error', { | ||
this.emitError({ | ||
code: 'subscribe_failed.subscription_limit' | ||
@@ -66,34 +54,65 @@ }); | ||
const subscriptionPromise = subscribe({ | ||
schema: this.config.schema, | ||
document: parse(query), | ||
variableValues: variables, | ||
contextValue: { ...this.config.context, | ||
subscribe: (...args) => { | ||
const source = this.config.subscriber.subscribe(...args); | ||
return AsyncUtils.filter(source, this.isAuthorized); | ||
} | ||
} | ||
}); | ||
this.subscriptions.set(id, subscriptionPromise); | ||
let result; | ||
try { | ||
result = await subscriptionPromise; | ||
} catch (err) { | ||
this.subscriptions.delete(id); | ||
throw err; | ||
} | ||
if (this.subscriptions.has(id)) { | ||
this.log('debug', 'Duplicate subscription attempted', { | ||
id | ||
}); | ||
this.emitError({ | ||
code: 'invalid_id.duplicate', | ||
data: id | ||
}); | ||
return; | ||
} | ||
if (result.errors != null) { | ||
this.subscriptions.delete(id); | ||
this.config.socket.emit('app_error', { | ||
code: 'subscribe_failed.gql_error', | ||
// $FlowFixMe | ||
data: result.errors | ||
const documentAST = parse(query); | ||
const validationRules = [...specifiedRules, ...(this.config.makeValidationRules ? this.config.makeValidationRules({ | ||
query, | ||
variables | ||
}) : [])]; | ||
const validationErrors = validate(this.config.schema, documentAST, validationRules); | ||
if (validationErrors.length) { | ||
this.emitError({ | ||
code: 'subscribe_failed.document_error', | ||
data: validationErrors | ||
}); | ||
return; | ||
} | ||
const subscriptionPromise = subscribe({ | ||
schema: this.config.schema, | ||
document: documentAST, | ||
variableValues: variables, | ||
contextValue: { ...this.config.context, | ||
subscribe: async (...args) => { | ||
const source = this.config.subscriber.subscribe(...args); | ||
const filtered = AsyncUtils.filter(source, this.isAuthorized); | ||
return filtered; | ||
} | ||
} | ||
}); | ||
return; | ||
this.subscriptions.set(id, subscriptionPromise); | ||
try { | ||
result = await subscriptionPromise; | ||
} catch (err) { | ||
this.subscriptions.delete(id); | ||
throw err; | ||
} | ||
if (result.errors != null) { | ||
this.subscriptions.delete(id); | ||
this.emitError({ | ||
code: 'subscribe_failed.gql_error', | ||
// $FlowFixMe | ||
data: result.errors | ||
}); | ||
return; | ||
} | ||
} finally { | ||
acknowledge(cb); | ||
} | ||
acknowledge(cb); | ||
const subscription = result; | ||
@@ -168,2 +187,6 @@ var _iteratorNormalCompletion = true; | ||
emitError(error) { | ||
this.config.socket.emit('app_error', error); | ||
} | ||
} |
@@ -33,3 +33,4 @@ import IoServer from 'socket.io'; | ||
hasPermission: this.config.hasPermission, | ||
createLogger: this.config.createLogger || defaultCreateLogger | ||
createLogger: this.config.createLogger || defaultCreateLogger, | ||
makeValidationRules: this.config.makeValidationRules | ||
}); | ||
@@ -36,0 +37,0 @@ }); |
@@ -37,3 +37,3 @@ "use strict"; | ||
this.log('error', err.message, err); | ||
this.config.socket.emit('app_error', { | ||
this.emitError({ | ||
code: 'invalid_authorization' | ||
@@ -51,14 +51,2 @@ }); | ||
}, cb) => { | ||
if (this.subscriptions.has(id)) { | ||
this.log('debug', 'Duplicate subscription attempted', { | ||
id | ||
}); | ||
this.config.socket.emit('app_error', { | ||
code: 'invalid_id.duplicate', | ||
detail: id | ||
}); | ||
acknowledge(cb); | ||
return; | ||
} | ||
if (this.config.maxSubscriptionsPerConnection != null && this.subscriptions.size >= this.config.maxSubscriptionsPerConnection) { | ||
@@ -68,3 +56,3 @@ this.log('error', 'Max Subscription limit reached', { | ||
}); | ||
this.config.socket.emit('app_error', { | ||
this.emitError({ | ||
code: 'subscribe_failed.subscription_limit' | ||
@@ -77,34 +65,65 @@ }); | ||
const subscriptionPromise = (0, _graphql.subscribe)({ | ||
schema: this.config.schema, | ||
document: (0, _graphql.parse)(query), | ||
variableValues: variables, | ||
contextValue: { ...this.config.context, | ||
subscribe: (...args) => { | ||
const source = this.config.subscriber.subscribe(...args); | ||
return AsyncUtils.filter(source, this.isAuthorized); | ||
} | ||
} | ||
}); | ||
this.subscriptions.set(id, subscriptionPromise); | ||
let result; | ||
try { | ||
result = await subscriptionPromise; | ||
} catch (err) { | ||
this.subscriptions.delete(id); | ||
throw err; | ||
} | ||
if (this.subscriptions.has(id)) { | ||
this.log('debug', 'Duplicate subscription attempted', { | ||
id | ||
}); | ||
this.emitError({ | ||
code: 'invalid_id.duplicate', | ||
data: id | ||
}); | ||
return; | ||
} | ||
if (result.errors != null) { | ||
this.subscriptions.delete(id); | ||
this.config.socket.emit('app_error', { | ||
code: 'subscribe_failed.gql_error', | ||
// $FlowFixMe | ||
data: result.errors | ||
const documentAST = (0, _graphql.parse)(query); | ||
const validationRules = [..._graphql.specifiedRules, ...(this.config.makeValidationRules ? this.config.makeValidationRules({ | ||
query, | ||
variables | ||
}) : [])]; | ||
const validationErrors = (0, _graphql.validate)(this.config.schema, documentAST, validationRules); | ||
if (validationErrors.length) { | ||
this.emitError({ | ||
code: 'subscribe_failed.document_error', | ||
data: validationErrors | ||
}); | ||
return; | ||
} | ||
const subscriptionPromise = (0, _graphql.subscribe)({ | ||
schema: this.config.schema, | ||
document: documentAST, | ||
variableValues: variables, | ||
contextValue: { ...this.config.context, | ||
subscribe: async (...args) => { | ||
const source = this.config.subscriber.subscribe(...args); | ||
const filtered = AsyncUtils.filter(source, this.isAuthorized); | ||
return filtered; | ||
} | ||
} | ||
}); | ||
return; | ||
this.subscriptions.set(id, subscriptionPromise); | ||
try { | ||
result = await subscriptionPromise; | ||
} catch (err) { | ||
this.subscriptions.delete(id); | ||
throw err; | ||
} | ||
if (result.errors != null) { | ||
this.subscriptions.delete(id); | ||
this.emitError({ | ||
code: 'subscribe_failed.gql_error', | ||
// $FlowFixMe | ||
data: result.errors | ||
}); | ||
return; | ||
} | ||
} finally { | ||
acknowledge(cb); | ||
} | ||
acknowledge(cb); | ||
const subscription = result; | ||
@@ -179,4 +198,8 @@ var _iteratorNormalCompletion = true; | ||
emitError(error) { | ||
this.config.socket.emit('app_error', error); | ||
} | ||
} | ||
exports.default = AuthorizedSocketConnection; |
@@ -43,3 +43,4 @@ "use strict"; | ||
hasPermission: this.config.hasPermission, | ||
createLogger: this.config.createLogger || defaultCreateLogger | ||
createLogger: this.config.createLogger || defaultCreateLogger, | ||
makeValidationRules: this.config.makeValidationRules | ||
}); | ||
@@ -46,0 +47,0 @@ }); |
{ | ||
"name": "@4c/graphql-subscription-server", | ||
"version": "0.0.3", | ||
"version": "0.1.0", | ||
"author": "4Catalyzer", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
74001
967