Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@hocuspocus/provider

Package Overview
Dependencies
Maintainers
4
Versions
104
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@hocuspocus/provider - npm Package Compare versions

Comparing version 1.1.0 to 2.0.0-alpha.0

dist/packages/provider/src/HocuspocusProviderWebsocket.d.ts

2

dist/packages/extension-monitor/src/Dashboard.d.ts
/// <reference types="node" />
import { IncomingMessage, ServerResponse } from 'http';
import { Socket } from 'net';
import WebSocket, { WebSocketServer } from 'ws';
import { Socket } from 'net';
import { Storage } from './Storage';

@@ -6,0 +6,0 @@ export interface Configuration {

/// <reference types="node" />
import { IncomingMessage, ServerResponse } from 'http';
import { Extension, onChangePayload, onConfigurePayload, onLoadDocumentPayload, onDisconnectPayload, onRequestPayload, onUpgradePayload, connectedPayload } from '@hocuspocus/server';
import { IncomingMessage, ServerResponse } from 'http';
import WebSocket from 'ws';

@@ -5,0 +5,0 @@ import { Storage } from './Storage';

import { HocuspocusProvider, HocuspocusProviderConfiguration } from './HocuspocusProvider';
export declare type HocuspocusCloudProviderConfiguration = Required<Pick<HocuspocusProviderConfiguration, 'name'>> & Partial<HocuspocusProviderConfiguration> & AdditionalHocuspocusCloudProviderConfiguration;
import { HocuspocusProviderWebsocketConfiguration } from './HocuspocusProviderWebsocket';
export declare type HocuspocusCloudProviderConfiguration = Required<Pick<HocuspocusProviderConfiguration, 'name'>> & Partial<HocuspocusProviderConfiguration> & Partial<Pick<HocuspocusProviderWebsocketConfiguration, 'url'>> & AdditionalHocuspocusCloudProviderConfiguration;
export interface AdditionalHocuspocusCloudProviderConfiguration {

@@ -4,0 +5,0 @@ /**

import * as Y from 'yjs';
import { Awareness } from 'y-protocols/awareness';
import * as mutex from 'lib0/mutex';
import type { Event, CloseEvent, MessageEvent } from 'ws';
import type { CloseEvent, Event, MessageEvent } from 'ws';
import EventEmitter from './EventEmitter';
import { ConstructableOutgoingMessage, onAuthenticationFailedParameters, onCloseParameters, onDisconnectParameters, onMessageParameters, onOpenParameters, onOutgoingMessageParameters, onStatelessParameters, onStatusParameters, onSyncedParameters, WebSocketStatus } from './types';
import { HocuspocusProviderWebsocket } from './HocuspocusProviderWebsocket';
import { onAwarenessChangeParameters, onAwarenessUpdateParameters } from '.';
export declare type HocuspocusProviderConfiguration = Required<Pick<CompleteHocuspocusProviderConfiguration, 'url' | 'name'>> & Partial<CompleteHocuspocusProviderConfiguration>;
export declare type HocuspocusProviderConfiguration = Required<Pick<CompleteHocuspocusProviderConfiguration, 'name' | 'websocketProvider'>> & Partial<CompleteHocuspocusProviderConfiguration>;
export interface CompleteHocuspocusProviderConfiguration {
/**
* URL of your @hocuspocus/server instance
*/
url: string;
/**
* The identifier/name of your document

@@ -23,6 +20,2 @@ */

/**
* Pass `false` to start the connection manually.
*/
connect: boolean;
/**
* Pass false to disable broadcasting between browser tabs.

@@ -46,5 +39,5 @@ */

/**
* An optional WebSocket polyfill, for example for Node.js
* Hocuspocus websocket provider
*/
WebSocketPolyfill: any;
websocketProvider: HocuspocusProviderWebsocket;
/**

@@ -54,38 +47,2 @@ * Force syncing the document in the defined interval.

forceSyncInterval: false | number;
/**
* Disconnect when no message is received for the defined amount of milliseconds.
*/
messageReconnectTimeout: number;
/**
* The delay between each attempt in milliseconds. You can provide a factor to have the delay grow exponentially.
*/
delay: number;
/**
* The intialDelay is the amount of time to wait before making the first attempt. This option should typically be 0 since you typically want the first attempt to happen immediately.
*/
initialDelay: number;
/**
* The factor option is used to grow the delay exponentially.
*/
factor: number;
/**
* The maximum number of attempts or 0 if there is no limit on number of attempts.
*/
maxAttempts: number;
/**
* minDelay is used to set a lower bound of delay when jitter is enabled. This property has no effect if jitter is disabled.
*/
minDelay: number;
/**
* The maxDelay option is used to set an upper bound for the delay when factor is enabled. A value of 0 can be provided if there should be no upper bound when calculating delay.
*/
maxDelay: number;
/**
* If jitter is true then the calculated delay will be a random integer value between minDelay and the calculated delay for the current iteration.
*/
jitter: boolean;
/**
* A timeout in milliseconds. If timeout is non-zero then a timer is set using setTimeout. If the timeout is triggered then future attempts will be aborted.
*/
timeout: number;
onAuthenticated: () => void;

@@ -113,28 +70,14 @@ onAuthenticationFailed: (data: onAuthenticationFailedParameters) => void;

subscribedToBroadcastChannel: boolean;
webSocket: WebSocket | null;
shouldConnect: boolean;
status: WebSocketStatus;
isSynced: boolean;
unsyncedChanges: number;
status: WebSocketStatus;
isAuthenticated: boolean;
lastMessageReceived: number;
mux: mutex.mutex;
intervals: any;
connectionAttempt: {
resolve: (value?: any) => void;
reject: (reason?: any) => void;
} | null;
constructor(configuration: HocuspocusProviderConfiguration);
onStatus({ status }: onStatusParameters): void;
setConfiguration(configuration?: Partial<HocuspocusProviderConfiguration>): void;
boundConnect: () => Promise<unknown>;
cancelWebsocketRetry?: () => void;
connect(): Promise<unknown>;
createWebSocketConnection(): Promise<unknown>;
resolveConnectionAttempt(): void;
stopConnectionAttempt(): void;
rejectConnectionAttempt(): void;
get document(): Y.Doc;
get awareness(): Awareness;
get hasUnsyncedChanges(): boolean;
checkConnection(): void;
forceSync(): void;

@@ -147,6 +90,2 @@ boundBeforeUnload: () => void;

awarenessUpdateHandler({ added, updated, removed }: any, origin: any): void;
permissionDeniedHandler(reason: string): void;
authenticatedHandler(): void;
get serverUrl(): string;
get url(): string;
get synced(): boolean;

@@ -160,6 +99,8 @@ set synced(state: boolean);

startSync(): void;
send(Message: ConstructableOutgoingMessage, args: any, broadcast?: boolean): void;
send(message: ConstructableOutgoingMessage, args: any, broadcast?: boolean): void;
onMessage(event: MessageEvent): void;
onClose(event: CloseEvent): void;
destroy(): void;
permissionDeniedHandler(reason: string): void;
authenticatedHandler(): void;
get broadcastChannel(): string;

@@ -166,0 +107,0 @@ boundBroadcastChannelSubscriber: (data: ArrayBuffer) => void;

@@ -10,6 +10,8 @@ import { Decoder } from 'lib0/decoding';

readVarUint(): MessageType;
readVarString(): string;
readVarUint8Array(): Uint8Array;
writeVarUint(type: MessageType): void;
writeVarString(string: string): void;
writeVarUint8Array(data: Uint8Array): void;
length(): number;
}
export * from './HocuspocusProvider';
export * from './HocuspocusCloudProvider';
export * from './HocuspocusProviderWebsocket';
export * from './types';

@@ -30,2 +30,3 @@ import { Awareness } from 'y-protocols/awareness';

export interface OutgoingMessageArguments {
documentName: string;
token: string;

@@ -32,0 +33,0 @@ document: Y.Doc;

/// <reference types="node" />
import { IncomingMessage as HTTPIncomingMessage } from 'http';
import AsyncLock from 'async-lock';
import WebSocket from 'ws';
import { IncomingMessage as HTTPIncomingMessage } from 'http';
import { CloseEvent } from '@hocuspocus/common';

@@ -65,13 +65,3 @@ import Document from './Document';

private handleMessage;
/**
* Get the underlying connection instance
* @deprecated
*/
get instance(): WebSocket;
/**
* Get the underlying connection instance
* @deprecated
*/
get connection(): WebSocket;
}
export default Connection;
/// <reference types="node" />
import { IncomingMessage, Server as HTTPServer } from 'http';
import WebSocket, { AddressInfo, WebSocketServer } from 'ws';
import { IncomingMessage, Server as HTTPServer } from 'http';
import { Configuration, HookName, HookPayload } from './types';
import { Configuration, HookName, HookPayload, onListenPayload } from './types';
import Document from './Document';
import { Debugger } from './Debugger';
import { onListenPayload } from '.';
export declare const defaultConfiguration: {

@@ -71,3 +70,3 @@ name: null;

*/
handleConnection(incoming: WebSocket, request: IncomingMessage, documentName: string, context?: any): void;
handleConnection(incoming: WebSocket, request: IncomingMessage, context?: any): void;
/**

@@ -102,6 +101,2 @@ * Handle update of the given document

private static getParameters;
/**
* Get document name by the given request
*/
private getDocumentNameFromRequest;
enableDebugging(): void;

@@ -108,0 +103,0 @@ enableMessageLogging(): void;

@@ -19,3 +19,4 @@ import { Decoder } from 'lib0/decoding';

writeVarUint(type: MessageType): void;
writeVarString(string: string): void;
get length(): number;
}

@@ -1,2 +0,1 @@

import { Awareness } from 'y-protocols/awareness';
import Connection from './Connection';

@@ -12,3 +11,3 @@ import { IncomingMessage } from './IncomingMessage';

readSyncMessage(message: IncomingMessage, document: Document, connection?: Connection, reply?: (message: Uint8Array) => void, requestFirstSync?: boolean): 0 | 2 | 1;
applyQueryAwarenessMessage(awareness: Awareness, reply?: (message: Uint8Array) => void): void;
applyQueryAwarenessMessage(document: Document, reply?: (message: Uint8Array) => void): void;
}

@@ -8,3 +8,3 @@ import { Encoder } from 'lib0/encoding';

category?: string;
constructor();
constructor(documentName: string);
createSyncMessage(): OutgoingMessage;

@@ -11,0 +11,0 @@ createSyncReplyMessage(): OutgoingMessage;

@@ -5,2 +5,3 @@ export * from './createDirectory';

export * from './newHocuspocusProvider';
export * from './newHocuspocusProviderWebsocket';
export * from './randomInteger';

@@ -7,0 +8,0 @@ export * from './redisConnectionSettings';

@@ -1,3 +0,3 @@

import { HocuspocusProvider, HocuspocusProviderConfiguration } from '@hocuspocus/provider';
import { HocuspocusProvider, HocuspocusProviderConfiguration, HocuspocusProviderWebsocketConfiguration } from '@hocuspocus/provider';
import { Hocuspocus } from '@hocuspocus/server';
export declare const newHocuspocusProvider: (server: Hocuspocus, options?: Partial<Omit<HocuspocusProviderConfiguration, 'url'>>) => HocuspocusProvider;
export declare const newHocuspocusProvider: (server: Hocuspocus, options?: Partial<HocuspocusProviderConfiguration>, websocketOptions?: Partial<HocuspocusProviderWebsocketConfiguration>) => HocuspocusProvider;
{
"name": "@hocuspocus/provider",
"version": "1.1.0",
"version": "2.0.0-alpha.0",
"description": "hocuspocus provider",

@@ -31,5 +31,5 @@ "homepage": "https://hocuspocus.dev",

"dependencies": {
"@hocuspocus/common": "^1.1.0",
"@hocuspocus/common": "^2.0.0-alpha.0",
"@lifeomic/attempt": "^3.0.2",
"lib0": "^0.2.46"
"lib0": "^0.2.47"
},

@@ -36,0 +36,0 @@ "peerDependencies": {

@@ -5,2 +5,6 @@ import {

} from './HocuspocusProvider'
import {
HocuspocusProviderWebsocket,
HocuspocusProviderWebsocketConfiguration,
} from './HocuspocusProviderWebsocket'

@@ -10,3 +14,4 @@ export type HocuspocusCloudProviderConfiguration =

Partial<HocuspocusProviderConfiguration> &
AdditionalHocuspocusCloudProviderConfiguration
Partial<Pick<HocuspocusProviderWebsocketConfiguration, 'url'>> &
AdditionalHocuspocusCloudProviderConfiguration

@@ -34,4 +39,6 @@ export interface AdditionalHocuspocusCloudProviderConfiguration {

configuration.websocketProvider = new HocuspocusProviderWebsocket({ url: configuration.url })
super(configuration as HocuspocusProviderConfiguration)
}
}
import * as Y from 'yjs'
import * as bc from 'lib0/broadcastchannel'
import * as time from 'lib0/time'
import { Awareness, removeAwarenessStates } from 'y-protocols/awareness'
import * as mutex from 'lib0/mutex'
import * as url from 'lib0/url'
import type { Event, CloseEvent, MessageEvent } from 'ws'
import { retry } from '@lifeomic/attempt'
import {
awarenessStatesToArray, Forbidden, Unauthorized, WsReadyStates,
} from '@hocuspocus/common'
import type { CloseEvent, Event, MessageEvent } from 'ws'
import { awarenessStatesToArray } from '@hocuspocus/common'
import EventEmitter from './EventEmitter'

@@ -23,16 +18,22 @@ import { IncomingMessage } from './IncomingMessage'

import {
ConstructableOutgoingMessage, onAuthenticationFailedParameters, onCloseParameters, onDisconnectParameters, onMessageParameters, onOpenParameters, onOutgoingMessageParameters, onStatelessParameters, onStatusParameters, onSyncedParameters, WebSocketStatus,
ConstructableOutgoingMessage,
onAuthenticationFailedParameters,
onCloseParameters,
onDisconnectParameters,
onMessageParameters,
onOpenParameters,
onOutgoingMessageParameters, onStatelessParameters,
onStatusParameters,
onSyncedParameters,
WebSocketStatus,
} from './types'
import { HocuspocusProviderWebsocket } from './HocuspocusProviderWebsocket'
import { StatelessMessage } from './OutgoingMessages/StatelessMessage'
import { onAwarenessChangeParameters, onAwarenessUpdateParameters } from '.'
import { StatelessMessage } from './OutgoingMessages/StatelessMessage'
export type HocuspocusProviderConfiguration =
Required<Pick<CompleteHocuspocusProviderConfiguration, 'url' | 'name'>>
Required<Pick<CompleteHocuspocusProviderConfiguration, 'name' | 'websocketProvider'>>
& Partial<CompleteHocuspocusProviderConfiguration>
export interface CompleteHocuspocusProviderConfiguration {
/**
* URL of your @hocuspocus/server instance
*/
url: string,
/**

@@ -46,7 +47,4 @@ * The identifier/name of your document

document: Y.Doc,
/**
* Pass `false` to start the connection manually.
*/
connect: boolean,
/**
* Pass false to disable broadcasting between browser tabs.

@@ -68,5 +66,5 @@ */

/**
* An optional WebSocket polyfill, for example for Node.js
* Hocuspocus websocket provider
*/
WebSocketPolyfill: any,
websocketProvider: HocuspocusProviderWebsocket,
/**

@@ -76,38 +74,3 @@ * Force syncing the document in the defined interval.

forceSyncInterval: false | number,
/**
* Disconnect when no message is received for the defined amount of milliseconds.
*/
messageReconnectTimeout: number,
/**
* The delay between each attempt in milliseconds. You can provide a factor to have the delay grow exponentially.
*/
delay: number,
/**
* The intialDelay is the amount of time to wait before making the first attempt. This option should typically be 0 since you typically want the first attempt to happen immediately.
*/
initialDelay: number,
/**
* The factor option is used to grow the delay exponentially.
*/
factor: number,
/**
* The maximum number of attempts or 0 if there is no limit on number of attempts.
*/
maxAttempts: number,
/**
* minDelay is used to set a lower bound of delay when jitter is enabled. This property has no effect if jitter is disabled.
*/
minDelay: number,
/**
* The maxDelay option is used to set an upper bound for the delay when factor is enabled. A value of 0 can be provided if there should be no upper bound when calculating delay.
*/
maxDelay: number,
/**
* If jitter is true then the calculated delay will be a random integer value between minDelay and the calculated delay for the current iteration.
*/
jitter: boolean,
/**
* A timeout in milliseconds. If timeout is non-zero then a timer is set using setTimeout. If the timeout is triggered then future attempts will be aborted.
*/
timeout: number,
onAuthenticated: () => void,

@@ -137,3 +100,2 @@ onAuthenticationFailed: (data: onAuthenticationFailedParameters) => void,

name: '',
url: '',
// @ts-ignore

@@ -143,26 +105,6 @@ document: undefined,

awareness: undefined,
WebSocketPolyfill: undefined,
token: null,
parameters: {},
connect: true,
broadcast: true,
forceSyncInterval: false,
// TODO: this should depend on awareness.outdatedTime
messageReconnectTimeout: 30000,
// 1 second
delay: 1000,
// instant
initialDelay: 0,
// double the delay each time
factor: 2,
// unlimited retries
maxAttempts: 0,
// wait at least 1 second
minDelay: 1000,
// at least every 30 seconds
maxDelay: 30000,
// randomize
jitter: true,
// retry forever
timeout: 0,
onAuthenticated: () => null,

@@ -187,16 +129,10 @@ onAuthenticationFailed: () => null,

webSocket: WebSocket | null = null
isSynced = false
shouldConnect = true
unsyncedChanges = 0
status = WebSocketStatus.Disconnected
isSynced = false
unsyncedChanges = 0
isAuthenticated = false
lastMessageReceived = 0
mux = mutex.createMutex()

@@ -206,10 +142,4 @@

forceSync: null,
connectionChecker: null,
}
connectionAttempt: {
resolve: (value?: any) => void
reject: (reason?: any) => void
} | null = null
constructor(configuration: HocuspocusProviderConfiguration) {

@@ -221,14 +151,7 @@ super()

this.configuration.awareness = configuration.awareness ? configuration.awareness : new Awareness(this.document)
this.configuration.WebSocketPolyfill = configuration.WebSocketPolyfill ? configuration.WebSocketPolyfill : WebSocket
this.on('open', this.configuration.onOpen)
this.on('authenticated', this.configuration.onAuthenticated)
this.on('authenticationFailed', this.configuration.onAuthenticationFailed)
this.on('connect', this.configuration.onConnect)
this.on('message', this.configuration.onMessage)
this.on('outgoingMessage', this.configuration.onOutgoingMessage)
this.on('synced', this.configuration.onSynced)
this.on('status', this.configuration.onStatus)
this.on('disconnect', this.configuration.onDisconnect)
this.on('close', this.configuration.onClose)
this.on('destroy', this.configuration.onDestroy)

@@ -239,2 +162,25 @@ this.on('awarenessUpdate', this.configuration.onAwarenessUpdate)

this.on('authenticated', this.configuration.onAuthenticated)
this.on('authenticationFailed', this.configuration.onAuthenticationFailed)
this.configuration.websocketProvider.on('connect', this.configuration.onConnect)
this.configuration.websocketProvider.on('connect', (e: Event) => this.emit('connect', e))
this.configuration.websocketProvider.on('open', this.onOpen.bind(this))
this.configuration.websocketProvider.on('open', (e: Event) => this.emit('open', e))
this.configuration.websocketProvider.on('message', this.onMessage.bind(this))
this.configuration.websocketProvider.on('close', this.onClose.bind(this))
this.configuration.websocketProvider.on('close', this.configuration.onClose)
this.configuration.websocketProvider.on('close', (e: Event) => this.emit('close', e))
this.configuration.websocketProvider.on('status', this.onStatus.bind(this))
this.configuration.websocketProvider.on('disconnect', this.configuration.onDisconnect)
this.configuration.websocketProvider.on('disconnect', (e: Event) => this.emit('disconnect', e))
this.configuration.websocketProvider.on('destroy', this.configuration.onDestroy)
this.configuration.websocketProvider.on('destroy', (e: Event) => this.emit('destroy', e))
this.awareness.on('update', () => {

@@ -252,7 +198,2 @@ this.emit('awarenessUpdate', { states: awarenessStatesToArray(this.awareness.getStates()) })

this.intervals.connectionChecker = setInterval(
this.checkConnection.bind(this),
this.configuration.messageReconnectTimeout / 10,
)
if (this.configuration.forceSyncInterval) {

@@ -265,11 +206,10 @@ this.intervals.forceSync = setInterval(

if (typeof configuration.connect !== 'undefined') {
this.shouldConnect = configuration.connect
}
this.configuration.websocketProvider.attach(this)
}
if (!this.shouldConnect) {
return
}
public onStatus({ status } : onStatusParameters) {
this.status = status
this.connect()
this.configuration.onStatus({ status })
this.emit('status', { status })
}

@@ -281,109 +221,2 @@

boundConnect = this.connect.bind(this)
cancelWebsocketRetry?: () => void
async connect() {
if (this.status === WebSocketStatus.Connected) {
return
}
// Always cancel any previously initiated connection retryer instances
if (this.cancelWebsocketRetry) {
this.cancelWebsocketRetry()
this.cancelWebsocketRetry = undefined
}
this.unsyncedChanges = 0 // set to 0 in case we got reconnected
this.shouldConnect = true
this.subscribeToBroadcastChannel()
const abortableRetry = () => {
let cancelAttempt = false
const retryPromise = retry(this.createWebSocketConnection.bind(this), {
delay: this.configuration.delay,
initialDelay: this.configuration.initialDelay,
factor: this.configuration.factor,
maxAttempts: this.configuration.maxAttempts,
minDelay: this.configuration.minDelay,
maxDelay: this.configuration.maxDelay,
jitter: this.configuration.jitter,
timeout: this.configuration.timeout,
beforeAttempt: context => {
if (!this.shouldConnect || cancelAttempt) {
context.abort()
}
},
}).catch((error: any) => {
// If we aborted the connection attempt then don’t throw an error
// ref: https://github.com/lifeomic/attempt/blob/master/src/index.ts#L136
if (error && error.code !== 'ATTEMPT_ABORTED') {
throw error
}
})
return {
retryPromise,
cancelFunc: () => {
cancelAttempt = true
},
}
}
const { retryPromise, cancelFunc } = abortableRetry()
this.cancelWebsocketRetry = cancelFunc
return retryPromise
}
createWebSocketConnection() {
return new Promise((resolve, reject) => {
if (this.webSocket) {
this.webSocket.close()
this.webSocket = null
}
// Init the WebSocket connection
const ws = new this.configuration.WebSocketPolyfill(this.url)
ws.binaryType = 'arraybuffer'
ws.onmessage = this.onMessage.bind(this)
ws.onclose = this.onClose.bind(this)
ws.onopen = this.onOpen.bind(this)
ws.onerror = (err: any) => {
reject(err)
}
this.webSocket = ws
// Reset the status
this.synced = false
this.status = WebSocketStatus.Connecting
this.emit('status', { status: WebSocketStatus.Connecting })
// Store resolve/reject for later use
this.connectionAttempt = {
resolve,
reject,
}
})
}
resolveConnectionAttempt() {
this.connectionAttempt?.resolve()
this.connectionAttempt = null
this.status = WebSocketStatus.Connected
this.emit('status', { status: WebSocketStatus.Connected })
this.emit('connect')
}
stopConnectionAttempt() {
this.connectionAttempt = null
}
rejectConnectionAttempt() {
this.connectionAttempt?.reject()
this.connectionAttempt = null
}
get document() {

@@ -401,29 +234,4 @@ return this.configuration.document

checkConnection() {
// Don’t check the connection when it’s not even established
if (this.status !== WebSocketStatus.Connected) {
return
}
// Don’t close then connection while waiting for the first message
if (!this.lastMessageReceived) {
return
}
// Don’t close the connection when a message was received recently
if (this.configuration.messageReconnectTimeout >= time.getUnixTime() - this.lastMessageReceived) {
return
}
// No message received in a long time, not even your own
// Awareness updates, which are updated every 15 seconds.
this.webSocket?.close()
}
forceSync() {
if (!this.webSocket) {
return
}
this.send(SyncStepOneMessage, { document: this.document })
this.send(SyncStepOneMessage, { document: this.document, documentName: this.configuration.name })
}

@@ -442,3 +250,2 @@

window.addEventListener('online', this.boundConnect)
window.addEventListener('beforeunload', this.boundBeforeUnload)

@@ -448,3 +255,3 @@ }

sendStateless(payload: string) {
this.send(StatelessMessage, { payload })
this.send(StatelessMessage, { documentName: this.configuration.name, payload })
}

@@ -458,3 +265,3 @@

this.unsyncedChanges += 1
this.send(UpdateMessage, { update }, true)
this.send(UpdateMessage, { update, documentName: this.configuration.name }, true)
}

@@ -468,33 +275,6 @@

clients: changedClients,
documentName: this.configuration.name,
}, true)
}
permissionDeniedHandler(reason: string) {
this.emit('authenticationFailed', { reason })
this.isAuthenticated = false
this.shouldConnect = false
}
authenticatedHandler() {
this.isAuthenticated = true
this.emit('authenticated')
this.startSync()
}
// Ensure that the URL always ends with /
get serverUrl() {
while (this.configuration.url[this.configuration.url.length - 1] === '/') {
return this.configuration.url.slice(0, this.configuration.url.length - 1)
}
return this.configuration.url
}
get url() {
const encodedParams = url.encodeQueryParams(this.configuration.parameters)
return `${this.serverUrl}/${this.configuration.name}${encodedParams.length === 0 ? '' : `?${encodedParams}`}`
}
get synced(): boolean {

@@ -523,14 +303,4 @@ return this.isSynced

disconnect() {
this.shouldConnect = false
this.disconnectBroadcastChannel()
if (this.webSocket === null) {
return
}
try {
this.webSocket.close()
} catch {
//
}
this.configuration.websocketProvider.detach(this)
}

@@ -544,2 +314,3 @@

token: await this.getToken(),
documentName: this.configuration.name,
})

@@ -562,3 +333,3 @@ return

startSync() {
this.send(SyncStepOneMessage, { document: this.document })
this.send(SyncStepOneMessage, { document: this.document, documentName: this.configuration.name })

@@ -569,2 +340,3 @@ if (this.awareness.getLocalState() !== null) {

clients: [this.document.clientID],
documentName: this.configuration.name,
})

@@ -574,24 +346,26 @@ }

send(Message: ConstructableOutgoingMessage, args: any, broadcast = false) {
send(message: ConstructableOutgoingMessage, args: any, broadcast = false) {
if (broadcast) {
this.mux(() => { this.broadcast(Message, args) })
this.mux(() => { this.broadcast(message, args) })
}
if (this.webSocket?.readyState === WsReadyStates.Open) {
const messageSender = new MessageSender(Message, args)
const messageSender = new MessageSender(message, args)
this.emit('outgoingMessage', { message: messageSender.message })
messageSender.send(this.webSocket)
}
this.emit('outgoingMessage', { message: messageSender.message })
messageSender.send(this.configuration.websocketProvider)
}
onMessage(event: MessageEvent) {
this.resolveConnectionAttempt()
const message = new IncomingMessage(event.data)
this.lastMessageReceived = time.getUnixTime()
const documentName = message.readVarString()
const message = new IncomingMessage(event.data)
if (documentName !== this.configuration.name) {
return // message is meant for another provider
}
this.emit('message', { event, message })
message.writeVarString(documentName)
this.emit('message', { event, message: new IncomingMessage(event.data) })
new MessageReceiver(message).apply(this)

@@ -601,57 +375,11 @@ }

onClose(event: CloseEvent) {
this.emit('close', { event })
this.webSocket = null
this.isAuthenticated = false
this.synced = false
if (this.status === WebSocketStatus.Connected) {
// update awareness (all users except local left)
removeAwarenessStates(
this.awareness,
Array.from(this.awareness.getStates().keys()).filter(client => client !== this.document.clientID),
this,
)
this.status = WebSocketStatus.Disconnected
this.emit('status', { status: WebSocketStatus.Disconnected })
this.emit('disconnect', { event })
}
if (event.code === Unauthorized.code) {
if (!this.configuration.quiet) {
console.warn('[HocuspocusProvider] An authentication token is required, but you didn’t send one. Try adding a `token` to your HocuspocusProvider configuration. Won’t try again.')
}
this.shouldConnect = false
}
if (event.code === Forbidden.code) {
if (!this.configuration.quiet) {
console.warn('[HocuspocusProvider] The provided authentication token isn’t allowed to connect to this server. Will try again.')
}
}
if (this.connectionAttempt) {
// That connection attempt failed.
this.rejectConnectionAttempt()
} else if (this.shouldConnect) {
// The connection was closed by the server. Let’s just try again.
this.connect()
}
// If we’ll reconnect, we’re done for now.
if (this.shouldConnect) {
return
}
// The status is set correctly already.
if (this.status === WebSocketStatus.Disconnected) {
return
}
// Let’s update the connection status.
this.status = WebSocketStatus.Disconnected
this.emit('status', { status: WebSocketStatus.Disconnected })
this.emit('disconnect', { event })
// update awareness (all users except local left)
removeAwarenessStates(
this.awareness,
Array.from(this.awareness.getStates().keys()).filter(client => client !== this.document.clientID),
this,
)
}

@@ -666,11 +394,4 @@

clearInterval(this.intervals.connectionChecker)
removeAwarenessStates(this.awareness, [this.document.clientID], 'provider destroy')
// If there is still a connection attempt outstanding then we should stop
// it before calling disconnect, otherwise it will be rejected in the onClose
// handler and trigger a retry
this.stopConnectionAttempt()
this.disconnect()

@@ -687,8 +408,21 @@

window.removeEventListener('online', this.boundConnect)
window.removeEventListener('beforeunload', this.boundBeforeUnload)
}
permissionDeniedHandler(reason: string) {
this.emit('authenticationFailed', { reason })
this.isAuthenticated = false
this.disconnect()
this.status = WebSocketStatus.Disconnected
}
authenticatedHandler() {
this.isAuthenticated = true
this.emit('authenticated')
this.startSync()
}
get broadcastChannel() {
return `${this.serverUrl}/${this.configuration.name}`
return `${this.configuration.name}`
}

@@ -701,2 +435,7 @@

const message = new IncomingMessage(data)
const documentName = message.readVarString()
message.writeVarString(documentName)
new MessageReceiver(message)

@@ -717,4 +456,4 @@ .setBroadcasted(true)

this.broadcast(SyncStepTwoMessage, { document: this.document })
this.broadcast(QueryAwarenessMessage)
this.broadcast(AwarenessMessage, { awareness: this.awareness, clients: [this.document.clientID] })
this.broadcast(QueryAwarenessMessage, { document: this.document })
this.broadcast(AwarenessMessage, { awareness: this.awareness, clients: [this.document.clientID], document: this.document })
})

@@ -729,2 +468,3 @@ }

states: new Map(),
documentName: this.configuration.name,
}, true)

@@ -731,0 +471,0 @@

@@ -5,2 +5,3 @@ import {

readVarUint8Array,
readVarString,
Decoder,

@@ -13,2 +14,3 @@ } from 'lib0/decoding'

writeVarUint8Array,
writeVarString,
length,

@@ -36,2 +38,6 @@ } from 'lib0/encoding'

readVarString(): string {
return readVarString(this.decoder)
}
readVarUint8Array() {

@@ -45,2 +51,6 @@ return readVarUint8Array(this.decoder)

writeVarString(string: string) {
return writeVarString(this.encoder, string)
}
writeVarUint8Array(data: Uint8Array) {

@@ -47,0 +57,0 @@ return writeVarUint8Array(this.encoder, data)

export * from './HocuspocusProvider'
export * from './HocuspocusCloudProvider'
export * from './HocuspocusProviderWebsocket'
export * from './types'

@@ -30,2 +30,4 @@ import * as awarenessProtocol from 'y-protocols/awareness'

const emptyMessageLength = message.length()
switch (type) {

@@ -57,3 +59,3 @@ case MessageType.Sync:

// Reply
if (message.length() > 1) {
if (message.length() > emptyMessageLength + 1) { // length of documentName (considered in emptyMessageLength plus length of yjs sync type, set in applySyncMessage)
if (this.broadcasted) {

@@ -85,3 +87,3 @@ // TODO: Some weird TypeScript error

// Synced once we receive Step2
if (emitSynced && (syncMessageType === messageYjsSyncStep2)) {
if (emitSynced && syncMessageType === messageYjsSyncStep2) {
provider.synced = true

@@ -88,0 +90,0 @@ }

@@ -1,2 +0,2 @@

import { writeVarUint } from 'lib0/encoding'
import { writeVarString, writeVarUint } from 'lib0/encoding'
import { writeAuthentication } from '@hocuspocus/common'

@@ -16,2 +16,3 @@ import { MessageType, OutgoingMessageArguments } from '../types'

writeVarString(this.encoder, args.documentName!)
writeVarUint(this.encoder, this.type)

@@ -18,0 +19,0 @@ writeAuthentication(this.encoder, args.token)

@@ -20,2 +20,3 @@ import * as encoding from 'lib0/encoding'

encoding.writeVarString(this.encoder, args.documentName!)
encoding.writeVarUint(this.encoder, this.type)

@@ -22,0 +23,0 @@

@@ -11,2 +11,7 @@ import * as encoding from 'lib0/encoding'

get(args: Partial<OutgoingMessageArguments>) {
console.log('queryAwareness: writing string docName', args.documentName)
console.log(this.encoder.cpos)
encoding.writeVarString(this.encoder, args.documentName!)
encoding.writeVarUint(this.encoder, this.type)

@@ -13,0 +18,0 @@

@@ -11,2 +11,3 @@ import { writeVarString, writeVarUint } from 'lib0/encoding'

get(args: Partial<OutgoingMessageArguments>) {
writeVarString(this.encoder, args.documentName!)
writeVarUint(this.encoder, this.type)

@@ -13,0 +14,0 @@ writeVarString(this.encoder, args.payload ?? '')

@@ -16,2 +16,3 @@ import * as encoding from 'lib0/encoding'

encoding.writeVarString(this.encoder, args.documentName!)
encoding.writeVarUint(this.encoder, this.type)

@@ -18,0 +19,0 @@ syncProtocol.writeSyncStep1(this.encoder, args.document)

@@ -1,2 +0,1 @@

import * as Y from 'yjs'
import * as encoding from 'lib0/encoding'

@@ -17,2 +16,3 @@ import * as syncProtocol from 'y-protocols/sync'

encoding.writeVarString(this.encoder, args.documentName!)
encoding.writeVarUint(this.encoder, this.type)

@@ -19,0 +19,0 @@ syncProtocol.writeSyncStep2(this.encoder, args.document)

@@ -1,2 +0,2 @@

import { writeVarUint } from 'lib0/encoding'
import { writeVarString, writeVarUint } from 'lib0/encoding'
import { writeUpdate } from 'y-protocols/sync'

@@ -12,3 +12,5 @@ import { MessageType, OutgoingMessageArguments } from '../types'

get(args: Partial<OutgoingMessageArguments>) {
writeVarString(this.encoder, args.documentName!)
writeVarUint(this.encoder, this.type)
writeUpdate(this.encoder, args.update)

@@ -15,0 +17,0 @@

@@ -34,2 +34,3 @@ import { Awareness } from 'y-protocols/awareness'

export interface OutgoingMessageArguments {
documentName: string,
token: string,

@@ -36,0 +37,0 @@ document: Y.Doc,

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 too big to display

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc