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

@hocuspocus/extension-database

Package Overview
Dependencies
Maintainers
3
Versions
87
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@hocuspocus/extension-database - npm Package Compare versions

Comparing version 1.0.0-alpha.5 to 1.0.0-alpha.6

dist/tests/extension-logger/onListen.d.ts

20

dist/hocuspocus-database.esm.js

@@ -12,4 +12,4 @@ import * as Y from 'yjs';

this.configuration = {
fetchUpdates: async () => [],
storeUpdate: async () => null,
fetch: async () => null,
store: async () => null,
};

@@ -25,7 +25,5 @@ this.configuration = {

async onLoadDocument({ document, documentName }) {
const updates = await this.configuration.fetchUpdates({ documentName });
if (updates && updates.length) {
updates.forEach((update) => {
Y.applyUpdate(document, update);
});
const update = await this.configuration.fetch({ documentName });
if (update) {
Y.applyUpdate(document, update);
}

@@ -37,5 +35,7 @@ return document;

*/
async onChange({ document, documentName }) {
const update = Buffer.from(Y.encodeStateAsUpdate(document));
return this.configuration.storeUpdate({ update, documentName });
async onStoreDocument(data) {
return this.configuration.store({
...data,
state: Buffer.from(Y.encodeStateAsUpdate(data.document)),
});
}

@@ -42,0 +42,0 @@ }

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

/// <reference types="node" />
import { Extension, onChangePayload, onLoadDocumentPayload } from '@hocuspocus/server';
import { Extension, onChangePayload, onLoadDocumentPayload, storePayload } from '@hocuspocus/server';
export interface DatabaseConfiguration {

@@ -8,12 +7,9 @@ /**

*/
fetchUpdates: ({ documentName }: {
fetch: ({ documentName }: {
documentName: string;
}) => Promise<Uint8Array[]>;
}) => Promise<Uint8Array | null>;
/**
* Pass a function to store updates in your database.
*/
storeUpdate: ({ update, documentName }: {
update: Buffer;
documentName: string;
}) => void;
store: (data: storePayload) => void;
}

@@ -36,3 +32,3 @@ export declare class Database implements Extension {

*/
onChange({ document, documentName }: onChangePayload): Promise<void>;
onStoreDocument(data: onChangePayload): Promise<void>;
}

@@ -18,2 +18,6 @@ import { Extension, onChangePayload, onConfigurePayload, onConnectPayload, onLoadDocumentPayload, onDestroyPayload, onDisconnectPayload, onRequestPayload, onUpgradePayload } from '@hocuspocus/server';

/**
* Whether to log something for the `onStoreDocument` hook.
*/
onStoreDocument: boolean;
/**
* Whether to log something for the `onConnect` hook.

@@ -57,2 +61,3 @@ */

onChange(data: onChangePayload): Promise<void>;
onStoreDocument(data: onDisconnectPayload): Promise<void>;
onConnect(data: onConnectPayload): Promise<void>;

@@ -59,0 +64,0 @@ onDisconnect(data: onDisconnectPayload): Promise<void>;

import { Database, DatabaseConfiguration } from '@hocuspocus/extension-database';
import sqlite3 from 'sqlite3';
export declare const schema = "CREATE TABLE IF NOT EXISTS \"documents\" (\n \"name\" varchar(255) NOT NULL,\n \"data\" blob NOT NULL,\n UNIQUE(name)\n)";
export declare const selectQuery = "\n SELECT data FROM \"documents\" WHERE name = $name ORDER BY rowid DESC\n";
export declare const upsertQuery = "\n INSERT INTO \"documents\" (\"name\", \"data\") VALUES ($name, $data)\n ON CONFLICT(name) DO UPDATE SET data = $data\n";
export interface SQLiteConfiguration extends DatabaseConfiguration {

@@ -20,8 +23,5 @@ /**

configuration: SQLiteConfiguration;
/**
* Constructor
*/
constructor(configuration?: Partial<SQLiteConfiguration>);
onConfigure(): Promise<void>;
onListen(): Promise<void>;
onConfigure(): Promise<void>;
}

@@ -6,3 +6,2 @@ /// <reference types="node" />

import { AxiosResponse } from 'axios';
import Timeout = NodeJS.Timeout;
export declare enum Events {

@@ -28,3 +27,3 @@ onChange = "change",

debounced: Map<string, {
timeout: Timeout;
timeout: NodeJS.Timeout;
start: number;

@@ -31,0 +30,0 @@ }>;

@@ -8,3 +8,3 @@ export default class EventEmitter {

off(event: string, fn?: Function): this;
protected removeAllListeners(): void;
removeAllListeners(): void;
}
import * as Y from 'yjs';
import { Awareness } from 'y-protocols/awareness';
import * as mutex from 'lib0/mutex';
import { Event, CloseEvent, MessageEvent } from 'ws';
import type { Event, CloseEvent, MessageEvent } from 'ws';
import EventEmitter from './EventEmitter';
import { OutgoingMessage } from './OutgoingMessage';
import { ConstructableOutgoingMessage } from './types';
import { onAwarenessChangeParameters, onAwarenessUpdateParameters } from '.';
export declare enum WebSocketStatus {

@@ -102,8 +103,10 @@ Connecting = "connecting",

onStatus: (status: any) => void;
onSynced: () => void;
onSynced: ({ state }: {
state: boolean;
}) => void;
onDisconnect: (event: CloseEvent) => void;
onClose: (event: CloseEvent) => void;
onDestroy: () => void;
onAwarenessUpdate: (states: any) => void;
onAwarenessChange: (states: any) => void;
onAwarenessUpdate: ({ states }: onAwarenessUpdateParameters) => void;
onAwarenessChange: ({ states }: onAwarenessChangeParameters) => void;
/**

@@ -110,0 +113,0 @@ * Don’t output any warnings.

@@ -35,1 +35,11 @@ import { Awareness } from 'y-protocols/awareness';

export declare type ConstructableOutgoingMessage = Constructable<AuthenticationMessage> | Constructable<AwarenessMessage> | Constructable<QueryAwarenessMessage> | Constructable<SyncStepOneMessage> | Constructable<SyncStepTwoMessage> | Constructable<UpdateMessage>;
export declare type onAwarenessUpdateParameters = {
states: StatesArray;
};
export declare type onAwarenessChangeParameters = {
states: StatesArray;
};
export declare type StatesArray = {
clientId: number;
[key: string | number]: any;
}[];

@@ -7,4 +7,4 @@ /// <reference types="node" />

import Document from './Document';
import { MessageLogger } from './Debugger';
declare class Connection {
import { Debugger } from './Debugger';
export declare class Connection {
webSocket: WebSocket;

@@ -21,7 +21,7 @@ context: any;

readOnly: Boolean;
debugger: MessageLogger;
logger: Debugger;
/**
* Constructor.
*/
constructor(connection: WebSocket, request: HTTPIncomingMessage, document: Document, timeout: number, socketId: string, context: any, readOnly?: boolean);
constructor(connection: WebSocket, request: HTTPIncomingMessage, document: Document, timeout: number, socketId: string, context: any, readOnly: boolean | undefined, logger: Debugger);
/**

@@ -28,0 +28,0 @@ * Set a callback that will be triggered when the connection is closed

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

export declare class MessageLogger {
export declare class Debugger {
logs: any[];

@@ -15,2 +15,1 @@ listen: boolean;

}
export declare const Debugger: MessageLogger;

@@ -6,4 +6,4 @@ import WebSocket from 'ws';

import Connection from './Connection';
import { MessageLogger } from './Debugger';
declare class Document extends Doc {
import { Debugger } from './Debugger';
export declare class Document extends Doc {
awareness: Awareness;

@@ -13,10 +13,13 @@ callbacks: {

};
connections: Map<any, any>;
connections: Map<WebSocket, {
clients: Set<any>;
connection: Connection;
}>;
name: string;
mux: mutex;
debugger: MessageLogger;
logger: Debugger;
/**
* Constructor.
*/
constructor(name: string);
constructor(name: string, logger: Debugger);
/**

@@ -23,0 +26,0 @@ * Check if the Document is empty

/// <reference types="node" />
import WebSocket, { WebSocketServer } from 'ws';
import WebSocket, { AddressInfo, WebSocketServer } from 'ws';
import { IncomingMessage, Server as HTTPServer } from 'http';
import { Configuration, Hook } from './types';
import Document from './Document';
import { MessageLogger } from './Debugger';
import { Debugger } from './Debugger';
import { onListenPayload } from '.';

@@ -12,2 +12,4 @@ export declare const defaultConfiguration: {

timeout: number;
debounce: number;
maxDebounce: number;
quiet: boolean;

@@ -23,3 +25,4 @@ };

webSocketServer?: WebSocketServer;
debugger: MessageLogger;
debugger: Debugger;
constructor(configuration?: Partial<Configuration>);
/**

@@ -33,3 +36,7 @@ * Configure the server

*/
listen(portOrCallback?: number | ((data: onListenPayload) => Promise<any>) | null, callback?: any): Promise<void>;
listen(portOrCallback?: number | ((data: onListenPayload) => Promise<any>) | null, callback?: any): Promise<Hocuspocus>;
get address(): AddressInfo;
get URL(): string;
get webSocketURL(): string;
get httpURL(): string;
private showStartScreen;

@@ -65,8 +72,14 @@ /**

* Handle update of the given document
* @private
*/
private handleDocumentUpdate;
timers: Map<string, {
timeout: NodeJS.Timeout;
start: number;
}>;
/**
* debounce the given function, using the given identifier
*/
debounce(id: string, func: Function, immediately?: boolean): void;
/**
* Create a new document by the given request
* @private
*/

@@ -76,8 +89,7 @@ private createDocument;

* Create a new connection by the given request and document
* @private
*/
private createConnection;
/**
* Run the given hook on all configured extensions
* Runs the given callback after each hook
* Run the given hook on all configured extensions.
* Runs the given callback after each hook.
*/

@@ -87,3 +99,2 @@ hooks(name: Hook, payload: any, callback?: Function | null): Promise<any>;

* Get parameters by the given request
* @private
*/

@@ -93,3 +104,2 @@ private static getParameters;

* Get document name by the given request
* @private
*/

@@ -96,0 +106,0 @@ private getDocumentNameFromRequest;

export * from './Hocuspocus';
export * from './Connection';
export * from './Document';
export * from './IncomingMessage';
export * from './OutgoingMessage';
export * from './types';
export * from './MessageReceiver';
export * from './Document';
export * from './Connection';

@@ -0,10 +1,13 @@

import { Awareness } from 'y-protocols/awareness';
import Connection from './Connection';
import { IncomingMessage } from './IncomingMessage';
import { MessageLogger } from './Debugger';
import { Debugger } from './Debugger';
import Document from './Document';
export declare class MessageReceiver {
message: IncomingMessage;
debugger: MessageLogger;
constructor(message: IncomingMessage);
apply(connection: Connection): void;
readSyncMessage(message: IncomingMessage, connection: Connection): 0 | 1 | 2;
logger: Debugger;
constructor(message: IncomingMessage, logger: Debugger);
apply(document: Document, connection?: Connection, reply?: (message: Uint8Array) => void): void;
readSyncMessage(message: IncomingMessage, document: Document, connection?: Connection, reply?: (message: Uint8Array) => void): 0 | 1 | 2;
applyQueryAwarenessMessage(awareness: Awareness, reply?: (message: Uint8Array) => void): void;
}

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

createAwarenessUpdateMessage(awareness: Awareness, changedClients?: Array<any>): OutgoingMessage;
writeQueryAwareness(): OutgoingMessage;
writeAuthenticated(): OutgoingMessage;

@@ -13,0 +14,0 @@ writePermissionDenied(reason: string): OutgoingMessage;

@@ -5,2 +5,3 @@ /// <reference types="node" />

import { Socket } from 'net';
import { Awareness } from 'y-protocols/awareness';
import Document from './Document';

@@ -12,3 +13,4 @@ import { Hocuspocus } from './Hocuspocus';

Awareness = 1,
Auth = 2
Auth = 2,
QueryAwareness = 3
}

@@ -36,6 +38,8 @@ /**

export interface Extension {
priority?: number;
onConfigure?(data: onConfigurePayload): Promise<any>;
onListen?(data: onListenPayload): Promise<any>;
onUpgrade?(data: onUpgradePayload): Promise<any>;
onConnect?(data: onConnectPayload): Promise<any>;
onAuthenticate?(data: onAuthenticatePayload): Promise<any>;
onChange?(data: onChangePayload): Promise<any>;
onConnect?(data: onConnectPayload): Promise<any>;
onConfigure?(data: onConfigurePayload): Promise<any>;
/**

@@ -46,13 +50,16 @@ * @deprecated onCreateDocument is deprecated, use onLoadDocument instead

onLoadDocument?(data: onLoadDocumentPayload): Promise<any>;
afterLoadDocument?(data: onLoadDocumentPayload): Promise<any>;
onChange?(data: onChangePayload): Promise<any>;
onStoreDocument?(data: onStoreDocumentPayload): Promise<any>;
afterStoreDocument?(data: afterStoreDocumentPayload): Promise<any>;
onAwarenessUpdate?(data: onAwarenessUpdatePayload): Promise<any>;
onRequest?(data: onRequestPayload): Promise<any>;
onDisconnect?(data: onDisconnectPayload): Promise<any>;
onDestroy?(data: onDestroyPayload): Promise<any>;
onDisconnect?(data: onDisconnectPayload): Promise<any>;
onListen?(data: onListenPayload): Promise<any>;
onRequest?(data: onRequestPayload): Promise<any>;
onUpgrade?(data: onUpgradePayload): Promise<any>;
}
export declare type Hook = 'onAuthenticate' | 'onChange' | 'onConnect' | 'onConfigure' |
export declare type Hook = 'onConfigure' | 'onListen' | 'onUpgrade' | 'onConnect' | 'onAuthenticate' |
/**
* @deprecated onCreateDocument is deprecated, use onLoadDocument instead
*/
'onCreateDocument' | 'onLoadDocument' | 'onDestroy' | 'onDisconnect' | 'onListen' | 'onRequest' | 'onUpgrade';
'onCreateDocument' | 'onLoadDocument' | 'afterLoadDocument' | 'onChange' | 'onStoreDocument' | 'afterStoreDocument' | 'onAwarenessUpdate' | 'onRequest' | 'onDisconnect' | 'onDestroy';
export interface Configuration extends Extension {

@@ -76,2 +83,11 @@ /**

/**
* Debounces the call of the `onStoreDocument` hook for the given amount of time in ms.
* Otherwise every single update would be persisted.
*/
debounce: number;
/**
* Makes sure to call `onStoreDocument` at least in the given amount of time (ms).
*/
maxDebounce: number;
/**
* By default, the servers show a start screen. If passed false, the server will start quietly.

@@ -83,8 +99,9 @@ */

*/
getDocumentName?(data: {
documentName: string;
request: IncomingMessage;
requestParameters: URLSearchParams;
}): string | Promise<string>;
getDocumentName?(data: getDocumentNamePayload): string | Promise<string>;
}
export interface getDocumentNamePayload {
documentName: string;
request: IncomingMessage;
requestParameters: URLSearchParams;
}
export interface onAuthenticatePayload {

@@ -118,2 +135,12 @@ documentName: string;

}
export interface afterLoadDocumentPayload {
context: any;
document: Document;
documentName: string;
instance: Hocuspocus;
requestHeaders: IncomingHttpHeaders;
requestParameters: URLSearchParams;
socketId: string;
connection: ConnectionConfiguration;
}
export interface onChangePayload {

@@ -130,2 +157,37 @@ clientsCount: number;

}
export interface onStoreDocumentPayload {
clientsCount: number;
context: any;
document: Document;
documentName: string;
instance: Hocuspocus;
requestHeaders: IncomingHttpHeaders;
requestParameters: URLSearchParams;
socketId: string;
}
export interface afterStoreDocumentPayload extends onStoreDocumentPayload {
}
export interface onAwarenessUpdatePayload {
clientsCount: number;
context: any;
document: Document;
documentName: string;
instance: Hocuspocus;
requestHeaders: IncomingHttpHeaders;
requestParameters: URLSearchParams;
update: Uint8Array;
socketId: string;
added: number[];
updated: number[];
removed: number[];
awareness: Awareness;
states: StatesArray;
}
export declare type StatesArray = {
clientId: number;
[key: string | number]: any;
}[];
export interface storePayload extends onStoreDocumentPayload {
state: Buffer;
}
export interface onDisconnectPayload {

@@ -132,0 +194,0 @@ clientsCount: number;

{
"name": "@hocuspocus/extension-database",
"description": "a generic Hocuspocus persistence driver for the database",
"version": "1.0.0-alpha.5",
"version": "1.0.0-alpha.6",
"homepage": "https://hocuspocus.dev",

@@ -29,4 +29,4 @@ "keywords": [

"dependencies": {
"@hocuspocus/server": "^1.0.0-alpha.91",
"yjs": "^13.5.22"
"@hocuspocus/server": "^1.0.0-alpha.92",
"yjs": "^13.5.24"
},

@@ -36,3 +36,3 @@ "publishConfig": {

},
"gitHead": "90e3f0955922515ccf1d643b7b43c4ad20387735"
"gitHead": "125d94dc3520bb5185293c9b316f66ddd9215992"
}

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

import { Extension, onChangePayload, onLoadDocumentPayload } from '@hocuspocus/server'
import {
Extension, onChangePayload, onLoadDocumentPayload, storePayload,
} from '@hocuspocus/server'
import * as Y from 'yjs'

@@ -9,7 +11,7 @@

*/
fetchUpdates: ({ documentName }: { documentName: string}) => Promise<Uint8Array[]>,
fetch: ({ documentName }: { documentName: string}) => Promise<Uint8Array | null>,
/**
* Pass a function to store updates in your database.
*/
storeUpdate: ({ update, documentName }: { update: Buffer, documentName: string}) => void,
store: (data: storePayload) => void,
}

@@ -22,4 +24,4 @@

configuration: DatabaseConfiguration = {
fetchUpdates: async () => [],
storeUpdate: async () => null,
fetch: async () => null,
store: async () => null,
}

@@ -41,8 +43,6 @@

async onLoadDocument({ document, documentName }: onLoadDocumentPayload): Promise<any> {
const updates = await this.configuration.fetchUpdates({ documentName })
const update = await this.configuration.fetch({ documentName })
if (updates && updates.length) {
updates.forEach((update: any) => {
Y.applyUpdate(document, update)
})
if (update) {
Y.applyUpdate(document, update)
}

@@ -56,9 +56,10 @@

*/
async onChange({ document, documentName }: onChangePayload) {
const update = Buffer.from(
Y.encodeStateAsUpdate(document),
)
return this.configuration.storeUpdate({ update, documentName })
async onStoreDocument(data: onChangePayload) {
return this.configuration.store({
...data,
state: Buffer.from(
Y.encodeStateAsUpdate(data.document),
),
})
}
}

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

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