graphql-ws
Advanced tools
Comparing version 0.1.2 to 0.1.3
@@ -79,4 +79,2 @@ import { ListenerFn } from 'eventemitter3'; | ||
private maxConnectTimeGenerator; | ||
private static arrayToBase64(array); | ||
private static base64ToArray(base64); | ||
constructor(url: string, options?: ClientOptions, webSocketImpl?: any); | ||
@@ -103,3 +101,2 @@ readonly status: any; | ||
private sendSingleFile(opId, file, offset?); | ||
private calcBinaryHash(file); | ||
private extractFiles(variables?); | ||
@@ -106,0 +103,0 @@ private binaryReader(resource, offset, onData, onComplete); |
@@ -21,3 +21,2 @@ "use strict"; | ||
const symbol_observable_1 = require("symbol-observable"); | ||
const sha256 = require("fast-sha256"); | ||
const protocol_1 = require("./protocol"); | ||
@@ -30,8 +29,2 @@ const defaults_1 = require("./defaults"); | ||
class SubscriptionClient { | ||
static arrayToBase64(array) { | ||
return window.btoa(String.fromCharCode(...new window.Uint8Array(array))); | ||
} | ||
static base64ToArray(base64) { | ||
return window.Uint8Array.from(window.atob(base64), (c) => c.charCodeAt(0)).buffer; | ||
} | ||
constructor(url, options, webSocketImpl) { | ||
@@ -273,3 +266,3 @@ const { connectionCallback = undefined, connectionParams = {}, timeout = defaults_1.WS_TIMEOUT, reconnect = false, reconnectionAttempts = Infinity, lazy = false, } = (options || {}); | ||
buffer.setUint32(8, file.getId(), true); | ||
file.startRead(offset); | ||
file.initRead(offset); | ||
let read; | ||
@@ -287,15 +280,2 @@ do { | ||
} | ||
calcBinaryHash(file) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const hash = new sha256.Hash(); | ||
file.startRead(); | ||
const buffer = new ArrayBuffer(common_1.BINARY_CHUNK_SIZE); | ||
let read; | ||
do { | ||
read = yield file.readInto(buffer, common_1.BINARY_CHUNK_SIZE); | ||
hash.update(new Uint8Array(buffer, 0, read)); | ||
} while (read === common_1.BINARY_CHUNK_SIZE); | ||
file.setHash(SubscriptionClient.arrayToBase64(hash.digest())); | ||
}); | ||
} | ||
extractFiles(variables) { | ||
@@ -305,3 +285,3 @@ return __awaiter(this, void 0, void 0, function* () { | ||
for (let file of common_1.findBinaries(variables || {})) { | ||
yield this.calcBinaryHash(file); | ||
yield file.initMetadata(); | ||
const found = files.find(f => f.equal(file)); | ||
@@ -332,3 +312,3 @@ if (found) { | ||
const obj = { opId, fileId: file.id }; | ||
const binary = new Binary_1.Binary(obj, this.binaryReader.bind(this), file); | ||
const binary = new Binary_1.Binary(obj, { onRead: this.binaryReader.bind(this) }, file); | ||
const found = this.filesIn[opId].find(f => f.binary.equal(binary)); | ||
@@ -335,0 +315,0 @@ if (found) { |
@@ -0,2 +1,4 @@ | ||
/// <reference types="node" /> | ||
import * as WebSocket from 'ws'; | ||
import { EventEmitter } from 'events'; | ||
import { ExecutionResult, GraphQLSchema, DocumentNode, ValidationContext, GraphQLFieldResolver } from 'graphql'; | ||
@@ -26,2 +28,3 @@ import { Binary } from './types/Binary'; | ||
}; | ||
filesOutEvent: EventEmitter; | ||
operations: { | ||
@@ -76,3 +79,2 @@ [opId: number]: ExecutionIterator; | ||
static create(options: ServerOptions, socketOptions: WebSocket.IServerOptions): SubscriptionServer; | ||
private static calcBinaryHash(file); | ||
constructor(options: ServerOptions, socketOptions: WebSocket.IServerOptions); | ||
@@ -79,0 +81,0 @@ readonly server: WebSocket.Server; |
@@ -12,2 +12,3 @@ "use strict"; | ||
const WebSocket = require("ws"); | ||
const events_1 = require("events"); | ||
const message_type_1 = require("./message-type"); | ||
@@ -22,3 +23,2 @@ const protocol_1 = require("./protocol"); | ||
const rxjs_1 = require("rxjs"); | ||
const crypto = require("crypto"); | ||
const Binary_1 = require("./types/Binary"); | ||
@@ -30,18 +30,2 @@ const common_1 = require("./common"); | ||
} | ||
static calcBinaryHash(file) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
if (file.getHash()) { | ||
return; | ||
} | ||
const hash = crypto.createHash('sha256'); | ||
file.startRead(); | ||
const buffer = new ArrayBuffer(common_1.BINARY_CHUNK_SIZE); | ||
let read; | ||
do { | ||
read = yield file.readInto(buffer, common_1.BINARY_CHUNK_SIZE); | ||
hash.update(Buffer.from(buffer, 0, read)); | ||
} while (read === common_1.BINARY_CHUNK_SIZE); | ||
file.setHash(hash.digest('base64')); | ||
}); | ||
} | ||
constructor(options, socketOptions) { | ||
@@ -70,2 +54,3 @@ const { onOperation, onOperationComplete, onConnect, onDisconnect, keepAlive, } = options; | ||
connectionContext.filesOut = {}; | ||
connectionContext.filesOutEvent = new events_1.EventEmitter(); | ||
if (this.keepAlive) { | ||
@@ -175,3 +160,3 @@ const keepAliveTimer = setInterval(() => { | ||
const obj = { connectionContext: ctx, opId, fileId: file.id }; | ||
const binary = new Binary_1.Binary(obj, this.binaryReader.bind(this), file); | ||
const binary = new Binary_1.Binary(obj, { onRead: this.binaryReader.bind(this) }, file); | ||
const found = ctx.filesIn[opId].find(f => f.binary.equal(binary)); | ||
@@ -195,3 +180,3 @@ if (found) { | ||
for (let file of common_1.findBinaries(response || {})) { | ||
yield SubscriptionServer.calcBinaryHash(file); | ||
yield file.initMetadata(); | ||
const found = files.find((f) => f.equal(file)); | ||
@@ -216,3 +201,3 @@ if (!found) { | ||
buffer.setUint32(8, file.getId(), true); | ||
file.startRead(); | ||
file.initRead(offset); | ||
let read; | ||
@@ -347,4 +332,6 @@ do { | ||
this.sendMessage(connectionContext, opId, message_type_1.MessageType.GQL_DATA, result); | ||
yield new Promise(() => { | ||
}); | ||
})).then(() => __awaiter(this, void 0, void 0, function* () { | ||
while (connectionContext.filesOut[opId].length > 0) { | ||
yield new Promise(resolve => connectionContext.filesOutEvent.once(opId.toString(), resolve)); | ||
} | ||
})) | ||
@@ -410,2 +397,3 @@ .then(() => { | ||
delete connectionContext.filesOut[opId][index]; | ||
connectionContext.filesOutEvent.emit(opId.toString()); | ||
} | ||
@@ -412,0 +400,0 @@ } |
/// <reference types="node" /> | ||
export declare type DataFunction = (data: Buffer | ArrayBuffer) => Promise<any>; | ||
export declare type CompleteFunction = (error?: any) => Promise<any>; | ||
export declare type BinaryReaderFunction = (resource: any, offset: number, onData: DataFunction, onComplete: CompleteFunction) => void; | ||
export declare type BinaryReadFunction = (resource: any, offset: number, onData: DataFunction, onComplete: CompleteFunction) => void; | ||
export declare type BinaryHashFunction = (resource: any) => Promise<string>; | ||
export declare type BinarySizeFunction = (resource: any) => Promise<number>; | ||
export interface SerializedBinary { | ||
@@ -9,11 +11,19 @@ ___binary: true; | ||
hash: string; | ||
size: number; | ||
} | ||
export interface BinaryOptions { | ||
onRead: BinaryReadFunction; | ||
onHash?: BinaryHashFunction; | ||
onSize?: BinarySizeFunction; | ||
} | ||
export declare class Binary { | ||
private static _id; | ||
private static readonly MAX_BUFFER_SIZE; | ||
private size; | ||
private buf; | ||
private bufPos; | ||
private bufSize; | ||
private resource; | ||
private reader; | ||
private onRead; | ||
private onHash; | ||
private onSize; | ||
private event; | ||
@@ -23,16 +33,14 @@ private eos; | ||
private id; | ||
private size; | ||
static isBinary(obj: Object): boolean; | ||
constructor(resource: any, reader: BinaryReaderFunction, serialized?: SerializedBinary); | ||
startRead(offset?: number): void; | ||
constructor(resource: any, options: BinaryOptions, serialized?: SerializedBinary); | ||
initRead(offset?: number): void; | ||
initMetadata(): Promise<void>; | ||
readInto(dest: ArrayBuffer, size: number, offset?: number): Promise<number>; | ||
getHash(): string; | ||
setHash(hash: string): void; | ||
getSize(): number; | ||
getId(): number; | ||
equal(binary: Binary): boolean; | ||
clone(source: Binary): void; | ||
toJSON(): { | ||
___binary: boolean; | ||
id: number; | ||
hash: string; | ||
}; | ||
toJSON(): SerializedBinary; | ||
private isValidData(data); | ||
@@ -39,0 +47,0 @@ private onData(data); |
@@ -18,13 +18,16 @@ "use strict"; | ||
class Binary { | ||
constructor(resource, reader, serialized) { | ||
this.size = 0; | ||
constructor(resource, options, serialized) { | ||
this.buf = []; | ||
this.bufPos = 0; | ||
this.bufSize = 0; | ||
this.eos = Symbol('EOS'); | ||
this.resource = resource; | ||
this.reader = reader; | ||
this.onRead = options.onRead; | ||
this.onSize = options.onSize; | ||
this.onHash = options.onHash; | ||
this.event = new events_1.EventEmitter(); | ||
this.eos = Symbol('EOS'); | ||
if (serialized && serialized.___binary) { | ||
this.hash = serialized.hash; | ||
this.id = serialized.id; | ||
this.size = serialized.size; | ||
} | ||
@@ -34,2 +37,3 @@ else { | ||
this.id = ++Binary._id; | ||
this.size = 0; | ||
} | ||
@@ -40,8 +44,28 @@ } | ||
} | ||
startRead(offset = 0) { | ||
this.size = 0; | ||
initRead(offset = 0) { | ||
this.buf = []; | ||
this.bufPos = 0; | ||
this.reader(this.resource, offset, this.onData.bind(this), this.onComplete.bind(this)); | ||
this.bufSize = 0; | ||
this.onRead(this.resource, offset, this.onData.bind(this), this.onComplete.bind(this)); | ||
} | ||
initMetadata() { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
if (!this.hash) { | ||
if (!this.onHash) { | ||
throw new Error('hash and onHash callback not defined'); | ||
} | ||
else { | ||
this.hash = yield this.onHash(this.resource); | ||
} | ||
} | ||
if (!this.size) { | ||
if (!this.onSize) { | ||
throw new Error('size and onSize callback not defined'); | ||
} | ||
else { | ||
this.size = yield this.onSize(this.resource); | ||
} | ||
} | ||
}); | ||
} | ||
readInto(dest, size, offset = 0) { | ||
@@ -78,3 +102,3 @@ return __awaiter(this, void 0, void 0, function* () { | ||
if (this.buf[0].byteLength === (this.bufPos + array.byteLength)) { | ||
this.size -= this.buf[0].byteLength; | ||
this.bufSize -= this.buf[0].byteLength; | ||
this.buf.shift(); | ||
@@ -98,4 +122,4 @@ this.bufPos = 0; | ||
} | ||
setHash(hash) { | ||
this.hash = hash; | ||
getSize() { | ||
return this.size; | ||
} | ||
@@ -106,17 +130,25 @@ getId() { | ||
equal(binary) { | ||
return this.hash === binary.hash; | ||
return (this === binary) || (this.hash && binary.hash && this.hash === binary.hash); | ||
} | ||
clone(source) { | ||
this.size = source.size; | ||
this.buf = source.buf; | ||
this.bufPos = source.bufPos; | ||
this.bufSize = source.bufSize; | ||
this.resource = source.resource; | ||
this.reader = source.reader; | ||
this.onRead = source.onRead; | ||
this.onHash = source.onHash; | ||
this.onSize = source.onSize; | ||
this.event = source.event; | ||
this.eos = source.eos; | ||
this.hash = source.hash; | ||
this.size = source.size; | ||
this.id = source.id; | ||
} | ||
toJSON() { | ||
return { ___binary: true, id: this.id, hash: this.hash }; | ||
return { | ||
___binary: true, | ||
id: this.id, | ||
hash: this.hash, | ||
size: this.size, | ||
}; | ||
} | ||
@@ -135,10 +167,9 @@ isValidData(data) { | ||
else { | ||
console.log('Unexpected data type: Buffer or ArrayBuffer expected'); | ||
return; | ||
throw new Error('Unexpected data type: Buffer or ArrayBuffer expected'); | ||
} | ||
if (data) { | ||
this.size += data.byteLength; | ||
this.bufSize += data.byteLength; | ||
} | ||
this.event.emit('added'); | ||
while (this.size > Binary.MAX_BUFFER_SIZE) { | ||
while (this.bufSize > Binary.MAX_BUFFER_SIZE) { | ||
yield this.waitForSpace(); | ||
@@ -145,0 +176,0 @@ } |
{ | ||
"name": "graphql-ws", | ||
"version": "0.1.2", | ||
"version": "0.1.3", | ||
"description": "WebSocket transport for GraphQL", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
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
389944
7255