@based/server
Advanced tools
Comparing version 2.8.10 to 2.9.0
@@ -7,2 +7,3 @@ import uws from '@based/uws'; | ||
import { Geo } from './types'; | ||
import { SharedConfigurationObservable } from './handlers/configuration/observable'; | ||
declare type Ua = { | ||
@@ -46,3 +47,3 @@ ua: string; | ||
subscriptions: { | ||
[id: string]: Subscription | SharedFunctionObservable | FunctionObservable; | ||
[id: string]: Subscription | SharedFunctionObservable | FunctionObservable | SharedConfigurationObservable; | ||
}; | ||
@@ -49,0 +50,0 @@ bpTimestamp: number; |
@@ -238,5 +238,9 @@ "use strict"; | ||
else { | ||
if (!this.socket.send(JSON.stringify(payload))) { | ||
// console.info('drain build bp') | ||
// buffer directly? | ||
if (typeof payload === 'string') { | ||
this.socket.send(payload); | ||
} | ||
else { | ||
this.socket.send(JSON.stringify(payload)); | ||
} | ||
} | ||
@@ -243,0 +247,0 @@ } |
@@ -9,6 +9,7 @@ import { SharedFunctionObservable } from './SharedObservable'; | ||
import { Subscription } from '../../subscription'; | ||
import { SharedConfigurationObservable } from '../../configuration/observable'; | ||
export { SharedFunctionObservable } from './SharedObservable'; | ||
export { FunctionObservable } from './Observable'; | ||
export declare const getObservable: (server: BasedServer, client: Client, [, id, payload, checksum, name]: SendSubscriptionGetDataMessage) => Promise<void>; | ||
export declare const subscribeFunction: (params: Params, name: string, payload: any, dataListener: DataListener) => Promise<SharedFunctionObservable | FunctionObservable | Subscription | void>; | ||
export declare const subscribeFunction: (params: Params, name: string, payload: any, dataListener: DataListener) => Promise<SharedFunctionObservable | FunctionObservable | Subscription | void | SharedConfigurationObservable>; | ||
export declare const subscribeObservable: (server: BasedServer, client: Client, [, id, payload, checksum, allwaysSend, name]: SubscribeMessage) => Promise<void>; |
@@ -11,3 +11,5 @@ /// <reference types="node" /> | ||
export declare class SharedFunctionObservable { | ||
lastDiff: [GenericObject, number]; | ||
lastDiff: number; | ||
jsonDiffCache: string; | ||
jsonCache: string; | ||
server: BasedServer; | ||
@@ -14,0 +16,0 @@ removeTimer: NodeJS.Timeout; |
@@ -5,2 +5,3 @@ "use strict"; | ||
const client_1 = require("@based/client"); | ||
const utils_1 = require("@saulx/utils"); | ||
const diff_1 = require("@saulx/diff"); | ||
@@ -44,3 +45,3 @@ const Params_1 = require("../../../Params"); | ||
}; | ||
console.error('Make observable error', this.name, err); | ||
// console.error('Make observable error', this.name, err) | ||
if (fn) { | ||
@@ -95,61 +96,61 @@ fn(null, 0, errObject); | ||
} | ||
let payload; | ||
if (this.state) { | ||
const s = this.state; | ||
const checksum = this.checksum; | ||
try { | ||
const diff = (0, diff_1.createPatch)(s, data); | ||
this.lastDiff = [diff, checksum]; | ||
payload = [ | ||
client_1.RequestTypes.SubscriptionDiff, | ||
this.id, | ||
diff, | ||
[checksum, version], | ||
]; | ||
if (version && version !== this.checksum) { | ||
let payload; | ||
if (this.state) { | ||
const s = this.state; | ||
const checksum = this.checksum; | ||
try { | ||
const diff = (0, diff_1.createPatch)(s, data); | ||
this.lastDiff = checksum; | ||
payload = this.jsonDiffCache = `[2,${this.id},${JSON.stringify(diff)},[${checksum},${version}]]`; | ||
} | ||
catch (err) { | ||
// cannot create patch | ||
console.error('cannot create patch', err); | ||
} | ||
} | ||
catch (err) { | ||
// cannot create patch | ||
console.error('cannot create patch', err); | ||
this.checksum = version; | ||
if (!payload) { | ||
if (this.lastDiff) { | ||
delete this.lastDiff; | ||
delete this.jsonDiffCache; | ||
} | ||
payload = `[1,${this.id},${JSON.stringify(data)},${version}]`; | ||
this.jsonCache = payload; | ||
} | ||
} | ||
if (!payload) { | ||
if (this.lastDiff) { | ||
delete this.lastDiff; | ||
else { | ||
this.jsonCache = `[1,${this.id},${JSON.stringify(data)},${version}]`; | ||
} | ||
payload = [client_1.RequestTypes.Subscription, this.id, data, version]; | ||
} | ||
// only do this is you see that it is the same data object (so first diff will not be a diff) | ||
// remove deep copy here -- way too heavy... | ||
// lets see... | ||
// deepCopy | ||
// this is such a big waste... | ||
// find something for this... | ||
if (!data) { | ||
console.warn('No data supplied - default to empty object', this.name, this.payload); | ||
} | ||
this.state = data || {}; | ||
this.checksum = version; | ||
if (this.dataListeners) { | ||
for (const id in this.dataListeners) { | ||
this.dataListeners[id].forEach((fn) => { | ||
fn(this.state, this.checksum); | ||
}); | ||
if (!data) { | ||
console.warn('No data supplied - default to empty object', this.name, this.payload); | ||
} | ||
} | ||
for (const id in this.clients) { | ||
const c = this.clients[id]; | ||
if (version !== c[1]) { | ||
c[1] = version; | ||
c[0].send(payload); | ||
if (c[2] === 1) { | ||
// have to make it different if it converts to a subscription | ||
this.unsubscribe(c[0]); | ||
this.state = (0, utils_1.deepCopy)(data) || {}; | ||
if (this.dataListeners) { | ||
for (const id in this.dataListeners) { | ||
this.dataListeners[id].forEach((fn) => { | ||
fn(this.state, this.checksum); | ||
}); | ||
} | ||
} | ||
else if (c[2]) { | ||
c[1] = version; | ||
c[0].send([client_1.RequestTypes.Subscription, this.id, null, version]); | ||
if (c[2] === 1) { | ||
this.unsubscribe(c[0]); | ||
let sameVersion; | ||
for (const id in this.clients) { | ||
const c = this.clients[id]; | ||
if (version !== c[1]) { | ||
c[1] = version; | ||
c[0].send(payload); | ||
if (c[2] === 1) { | ||
// have to make it different if it converts to a subscription | ||
this.unsubscribe(c[0]); | ||
} | ||
} | ||
else if (c[2]) { | ||
if (!sameVersion) { | ||
sameVersion = `[1,${this.id},null,${version}]`; | ||
} | ||
c[1] = version; | ||
c[0].send(sameVersion); | ||
if (c[2] === 1) { | ||
this.unsubscribe(c[0]); | ||
} | ||
} | ||
} | ||
@@ -243,4 +244,3 @@ } | ||
else if (this.state) { | ||
if (checksum === this.checksum) { | ||
// console.info('got version dont re-send') | ||
if (checksum && checksum === this.checksum) { | ||
if (get) { | ||
@@ -263,21 +263,9 @@ const payload = [ | ||
// this send has to be checked dont want to resend if it immediatly updates from the sub | ||
if (this.lastDiff && this.lastDiff[1] === checksum) { | ||
const payload = [ | ||
client_1.RequestTypes.SubscriptionDiff, | ||
this.id, | ||
this.lastDiff[0], | ||
[this.lastDiff[1], checksum], | ||
]; | ||
if (checksum && this.lastDiff === checksum) { | ||
isSend = true; | ||
client.send(payload); | ||
client.send(this.jsonDiffCache); | ||
} | ||
else { | ||
const payload = [ | ||
client_1.RequestTypes.Subscription, | ||
this.id, | ||
this.state, | ||
this.checksum, | ||
]; | ||
isSend = true; | ||
client.send(payload); | ||
client.send(this.jsonCache); | ||
} | ||
@@ -298,3 +286,2 @@ } | ||
sendData(client) { | ||
// this is easy to emulate | ||
if (this.checksum && !this.errorState) { | ||
@@ -304,9 +291,3 @@ const store = this.clients[client.id]; | ||
store[1] = this.checksum; | ||
const payload = [ | ||
client_1.RequestTypes.Subscription, | ||
this.id, | ||
this.state, | ||
this.checksum, | ||
]; | ||
client.send(payload); | ||
client.send(this.jsonCache); | ||
} | ||
@@ -313,0 +294,0 @@ } |
import uws from '@based/uws'; | ||
import { ResponseData } from '@based/client'; | ||
declare const _default: (res: uws.HttpResponse, payload: ResponseData, type: 0 | 1) => void; | ||
declare const _default: (res: uws.HttpResponse, payload: ResponseData | string, type: 0 | 1, version?: number) => void; | ||
export default _default; |
@@ -23,3 +23,3 @@ "use strict"; | ||
}; | ||
exports.default = (res, payload, type) => { | ||
exports.default = (res, payload, type, version) => { | ||
if (res.aborted) { | ||
@@ -29,57 +29,93 @@ return; | ||
let result; | ||
const reqType = payload[0]; | ||
if (reqType === client_1.RequestTypes.Get) { | ||
if (payload[3]) { | ||
invalid(res, payload[3], 'Invalid Query'); | ||
let reqType; | ||
let isParsed = false; | ||
if (typeof payload === 'string') { | ||
let secondIndex = 0; | ||
let lastIndex = 0; | ||
const len = payload.length - 1; | ||
for (let i = 4; i < 20; i++) { | ||
const s = payload[i]; | ||
if (!secondIndex && s === ',') { | ||
secondIndex = i; | ||
if (lastIndex) { | ||
break; | ||
} | ||
} | ||
if (!lastIndex) { | ||
if (payload[len - i] === ',') { | ||
lastIndex = len - i; | ||
if (secondIndex) { | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
if (!secondIndex && !lastIndex) { | ||
invalid(res, 'Invalid Request'); | ||
return; | ||
} | ||
else { | ||
const r = payload[2]; | ||
const checksum = (0, hash_1.hashObjectIgnoreKeyOrder)(r); | ||
ok(res); | ||
res.writeHeader('ETag', String(checksum)); | ||
res.writeHeader('Cache-Control', 'max-age=0, must-revalidate'); | ||
result = r; | ||
} | ||
result = payload.slice(secondIndex + 1, lastIndex); | ||
const checksum = version || (0, hash_1.hash)(result); | ||
ok(res); | ||
res.writeHeader('ETag', String(checksum)); | ||
res.writeHeader('Cache-Control', 'max-age=0, must-revalidate'); | ||
isParsed = true; | ||
} | ||
else if (reqType === client_1.RequestTypes.Set || | ||
reqType === client_1.RequestTypes.Copy || | ||
reqType === client_1.RequestTypes.Configuration || | ||
reqType === client_1.RequestTypes.Digest || | ||
reqType === client_1.RequestTypes.GetConfiguration || | ||
reqType === client_1.RequestTypes.Delete) { | ||
if (payload[3]) { | ||
invalid(res, payload[3]); | ||
return; | ||
else { | ||
reqType = payload[0]; | ||
if (reqType === client_1.RequestTypes.Get) { | ||
if (payload[3]) { | ||
invalid(res, payload[3], 'Invalid Query'); | ||
return; | ||
} | ||
else { | ||
const r = payload[2]; | ||
const checksum = (0, hash_1.hashObjectIgnoreKeyOrder)(r); | ||
ok(res); | ||
res.writeHeader('ETag', String(checksum)); | ||
res.writeHeader('Cache-Control', 'max-age=0, must-revalidate'); | ||
result = r; | ||
} | ||
} | ||
else { | ||
ok(res); | ||
result = payload[2]; | ||
else if (reqType === client_1.RequestTypes.Set || | ||
reqType === client_1.RequestTypes.Copy || | ||
reqType === client_1.RequestTypes.Configuration || | ||
reqType === client_1.RequestTypes.Digest || | ||
reqType === client_1.RequestTypes.GetConfiguration || | ||
reqType === client_1.RequestTypes.Delete) { | ||
if (payload[3]) { | ||
invalid(res, payload[3]); | ||
return; | ||
} | ||
else { | ||
ok(res); | ||
result = payload[2]; | ||
} | ||
} | ||
} | ||
else if (reqType === client_1.RequestTypes.Call) { | ||
if (payload[3]) { | ||
invalid(res, payload[3]); | ||
return; | ||
else if (reqType === client_1.RequestTypes.Call) { | ||
if (payload[3]) { | ||
invalid(res, payload[3]); | ||
return; | ||
} | ||
else { | ||
ok(res); | ||
result = payload[2]; | ||
} | ||
} | ||
else { | ||
ok(res); | ||
result = payload[2]; | ||
else if (reqType === client_1.RequestTypes.Subscription) { | ||
if (payload[4]) { | ||
invalid(res, payload[4]); | ||
return; | ||
} | ||
else { | ||
ok(res); | ||
res.writeHeader('ETag', String(payload[3])); | ||
res.writeHeader('Cache-Control', 'max-age=0, must-revalidate'); | ||
result = payload[2]; | ||
} | ||
} | ||
} | ||
else if (reqType === client_1.RequestTypes.Subscription) { | ||
if (payload[4]) { | ||
invalid(res, payload[4]); | ||
return; | ||
} | ||
else { | ||
ok(res); | ||
res.writeHeader('ETag', String(payload[3])); | ||
res.writeHeader('Cache-Control', 'max-age=0, must-revalidate'); | ||
result = payload[2]; | ||
invalid(res, 'Invalid Request'); | ||
} | ||
} | ||
else { | ||
invalid(res, 'Invalid Request'); | ||
} | ||
if (reqType === client_1.RequestTypes.Digest) { | ||
@@ -100,3 +136,3 @@ res.writeHeader('Content-Type', 'text/plain'); | ||
if (type === 0) { | ||
parsed = JSON.stringify(result); | ||
parsed = isParsed ? result : JSON.stringify(result); | ||
} | ||
@@ -135,3 +171,3 @@ else { | ||
csvParser.pipe(compressor); | ||
csvParser.write(Buffer.from(JSON.stringify(result))); | ||
csvParser.write(Buffer.from(isParsed ? result : JSON.stringify(result))); | ||
csvParser.end(); | ||
@@ -162,3 +198,3 @@ } | ||
if (!res.aborted) { | ||
csvParser.write(Buffer.from(JSON.stringify(result))); | ||
csvParser.write(Buffer.from(isParsed ? result : JSON.stringify(result))); | ||
csvParser.end(); | ||
@@ -165,0 +201,0 @@ } |
@@ -7,7 +7,6 @@ /// <reference types="node" /> | ||
import { DataListener } from '../../types'; | ||
declare type GenericObject = { | ||
[key: string]: any; | ||
}; | ||
export declare class Subscription { | ||
lastDiff: [GenericObject, number]; | ||
lastDiff: number; | ||
jsonDiffCache: string; | ||
jsonCache: string; | ||
checksum: number; | ||
@@ -35,2 +34,1 @@ server: BasedServer; | ||
export declare const unsubscribe: (server: BasedServer, client: Client, [, subscriptionId]: UnsubscribeMessage) => void; | ||
export {}; |
@@ -5,2 +5,3 @@ "use strict"; | ||
const client_1 = require("@based/client"); | ||
// type GenericObject = { [key: string]: any } | ||
class Subscription { | ||
@@ -36,9 +37,15 @@ constructor(server, id, query) { | ||
if (diff) { | ||
this.lastDiff = [diff, this.checksum]; | ||
payload = [ | ||
client_1.RequestTypes.SubscriptionDiff, | ||
this.id, | ||
diff, | ||
[this.checksum, checksum], | ||
]; | ||
this.lastDiff = this.checksum; | ||
if (isSchemaSubscription) { | ||
payload = this.jsonDiffCache = `[2,${this.id},${JSON.stringify(diff)},["${this.checksum}","${checksum}"]]`; | ||
} | ||
else { | ||
payload = this.jsonDiffCache = `[2,${this.id},${JSON.stringify(diff)},[${this.checksum},${checksum}]]`; | ||
} | ||
if (isSchemaSubscription) { | ||
this.jsonCache = `[1,${this.id},${JSON.stringify(data)},"${checksum}"]`; | ||
} | ||
else { | ||
this.jsonCache = `[1,${this.id},${JSON.stringify(data)},${checksum}]`; | ||
} | ||
} | ||
@@ -48,4 +55,10 @@ else { | ||
delete this.lastDiff; | ||
delete this.jsonDiffCache; | ||
} | ||
payload = [client_1.RequestTypes.Subscription, this.id, data, checksum]; | ||
if (isSchemaSubscription) { | ||
payload = this.jsonCache = `[1,${this.id},${JSON.stringify(data)},"${checksum}"]`; | ||
} | ||
else { | ||
payload = this.jsonCache = `[1,${this.id},${JSON.stringify(data)},${checksum}]`; | ||
} | ||
} | ||
@@ -58,2 +71,4 @@ for (const id in this.clients) { | ||
} | ||
// else if waiting for initial get... ? | ||
// TODO: may need fix | ||
} | ||
@@ -95,3 +110,5 @@ this.checksum = checksum; | ||
} | ||
// eslint-disable-next-line | ||
unsubscribeDataListener(client, fn, id) { } | ||
// eslint-disable-next-line | ||
subscribeDataListener(client, fn, id) { } | ||
@@ -145,19 +162,19 @@ unsubscribe(client) { | ||
// this send has to be checked dont want to resend if it immediatly updates from the sub | ||
if (this.lastDiff && this.lastDiff[1] === checksum) { | ||
const payload = [ | ||
client_1.RequestTypes.SubscriptionDiff, | ||
this.id, | ||
this.lastDiff[0], | ||
[this.lastDiff[1], this.observable.version], | ||
]; | ||
client.send(payload); | ||
if (this.lastDiff && this.lastDiff === checksum) { | ||
// const payload: SubscriptionDiffData = [ | ||
// RequestTypes.SubscriptionDiff, | ||
// this.id, | ||
// this.lastDiff[0], | ||
// [this.lastDiff[1], this.observable.version], | ||
// ] | ||
client.send(this.jsonDiffCache); | ||
} | ||
else { | ||
const payload = [ | ||
client_1.RequestTypes.Subscription, | ||
this.id, | ||
this.observable.cache, | ||
this.observable.version, | ||
]; | ||
client.send(payload); | ||
// const payload: SubscriptionData = [ | ||
// RequestTypes.Subscription, | ||
// this.id, | ||
// this.observable.cache, | ||
// this.observable.version, | ||
// ] | ||
client.send(this.jsonCache); | ||
} | ||
@@ -175,9 +192,9 @@ } | ||
store[1] = this.observable.version; | ||
const payload = [ | ||
client_1.RequestTypes.Subscription, | ||
this.id, | ||
this.observable.cache, | ||
this.observable.version, | ||
]; | ||
client.send(payload); | ||
// const payload: SubscriptionData = [ | ||
// RequestTypes.Subscription, | ||
// this.id, | ||
// this.observable.cache, | ||
// this.observable.version, | ||
// ] | ||
client.send(this.jsonCache); | ||
} | ||
@@ -184,0 +201,0 @@ } |
{ | ||
"name": "@based/server", | ||
"version": "2.8.10", | ||
"version": "2.9.0", | ||
"license": "MIT", | ||
@@ -13,6 +13,6 @@ "main": "dist/index.js", | ||
"peerDependencies": { | ||
"@saulx/selva": "10||11||12||13" | ||
"@saulx/selva": "10||11||12||13||14" | ||
}, | ||
"dependencies": { | ||
"@based/client": "2.7.4", | ||
"@based/client": "2.8.0", | ||
"@based/graphql": "2.4.2", | ||
@@ -19,0 +19,0 @@ "@based/uws": "2.4.0", |
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
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
344229
5461
+ Added@based/client@2.8.0(transitive)
+ Added@based/get-service@2.4.3(transitive)
+ Added@saulx/selva@14.2.2(transitive)
- Removed@based/client@2.7.4(transitive)
- Removed@based/get-service@2.4.2(transitive)
- Removed@saulx/selva@13.2.0(transitive)
Updated@based/client@2.8.0