@mescius/js-collaboration 18.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +18 -0
- package/dist/index.d.ts +195 -0
- package/dist/index.js +1 -0
- package/package.json +33 -0
- package/wrapper.mjs +5 -0
package/README.md
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
# SpreadJS Collaboration Plugin
|
|
2
|
+
|
|
3
|
+
The js collaboration package is one of the plugins for [SpreadJS](https://developer.mescius.com/spreadjs).
|
|
4
|
+
|
|
5
|
+
Extends SpreadJS with support for collaboration
|
|
6
|
+
|
|
7
|
+
For a detailed listing of SpreadJS sub-libraries and plug-ins, please see [Using SpreadJS Libraries](https://developer.mescius.com/spreadjs/docs/getstarted/modules).
|
|
8
|
+
|
|
9
|
+
## Installation
|
|
10
|
+
```sh
|
|
11
|
+
npm install @mescius/js-collaboration
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
## Getting more help
|
|
15
|
+
Visit the SpreadJS home page to get more info about the library:
|
|
16
|
+
[https://developer.mescius.com/spreadjs](https://developer.mescius.com/spreadjs)
|
|
17
|
+
|
|
18
|
+
You can ask any question about SpreadJS using the [SpreadJS Forum](https://developer.mescius.com/forums/spreadjs).
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
import { Server as HTTPServer } from "http";
|
|
2
|
+
import { Server as HTTPSServer } from "https";
|
|
3
|
+
import { Http2SecureServer, Http2Server } from "http2";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* The collaboration server, provides bidirectional communication, middleware and hooks mechanism.
|
|
7
|
+
*/
|
|
8
|
+
export declare class Server {
|
|
9
|
+
constructor(config?: IServerConfig);
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Register middlewares to the server
|
|
13
|
+
* @param middlewares - middlewares to register
|
|
14
|
+
*/
|
|
15
|
+
use(middlewares: IMiddlewares): void;
|
|
16
|
+
/**
|
|
17
|
+
* Register middleware to the server
|
|
18
|
+
* @param action - action name
|
|
19
|
+
* @param middleware - middleware to register
|
|
20
|
+
*/
|
|
21
|
+
use<K extends keyof IMiddlewareContext>(action: K, middleware: IMiddleware<IMiddlewareContext[K]>): void;
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Register hooks to the server
|
|
25
|
+
* @param hooks - hooks to register
|
|
26
|
+
*/
|
|
27
|
+
on(hooks: IHooks): void;
|
|
28
|
+
/**
|
|
29
|
+
* Register hooks to the server
|
|
30
|
+
* @param action - action name
|
|
31
|
+
* @param hook - hook to register
|
|
32
|
+
*/
|
|
33
|
+
on<K extends keyof IHookContext>(action: K, hook: IHook<IHookContext[K]>): void;
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Register feature to the server
|
|
37
|
+
* @param feature - feature to register
|
|
38
|
+
*/
|
|
39
|
+
useFeature(feature: IFeature): void;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export interface IServerConfig {
|
|
43
|
+
httpServer?: HTTPServer | HTTPSServer | Http2SecureServer | Http2Server;
|
|
44
|
+
port?: number;
|
|
45
|
+
/**
|
|
46
|
+
* The request path of the server. default is '/collaboration/'
|
|
47
|
+
*/
|
|
48
|
+
path?: string;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export interface IMiddleware<T> {
|
|
52
|
+
(context: T, next: INext): Promise<void>;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export interface IMiddlewares {
|
|
56
|
+
connect?: IMiddleware<IConnectMiddlewareContext>,
|
|
57
|
+
message?: IMiddleware<IMessageMiddlewareContext>
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export interface IMiddlewareContext {
|
|
61
|
+
connect: IConnectMiddlewareContext,
|
|
62
|
+
message: IMessageMiddlewareContext
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export interface IConnectMiddlewareContext {
|
|
66
|
+
connection: Connection;
|
|
67
|
+
roomId: string;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export interface IMessageMiddlewareContext {
|
|
71
|
+
roomId: string;
|
|
72
|
+
connection: Connection;
|
|
73
|
+
type: MessageType;
|
|
74
|
+
data: MessageData;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export type MessageType = string | null;
|
|
78
|
+
export type MessageData = unknown;
|
|
79
|
+
|
|
80
|
+
export interface INext {
|
|
81
|
+
(error?: Error | string): Promise<void>;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export interface IHook<T> {
|
|
85
|
+
(context: T): void;
|
|
86
|
+
}
|
|
87
|
+
export interface IHooks {
|
|
88
|
+
connect?: IHook<IConnectContext>,
|
|
89
|
+
message?: IHook<IMessageContext>,
|
|
90
|
+
createRoom?: IHook<ICreateRoomContext>,
|
|
91
|
+
disconnect?: IHook<IDisconnectContext>,
|
|
92
|
+
enterRoom?: IHook<IEnterRoomContext>,
|
|
93
|
+
leaveRoom?: IHook<ILeaveRoomContext>,
|
|
94
|
+
destroyRoom?: IHook<IDestroyRoomContext>,
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export interface IHookContext {
|
|
98
|
+
connect: IConnectContext,
|
|
99
|
+
createRoom: ICreateRoomContext,
|
|
100
|
+
enterRoom: IEnterRoomContext,
|
|
101
|
+
message: IMessageContext,
|
|
102
|
+
disconnect: IDisconnectContext,
|
|
103
|
+
leaveRoom: ILeaveRoomContext,
|
|
104
|
+
destroyRoom: IDestroyRoomContext,
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
export interface IConnectContext {
|
|
108
|
+
roomId: string;
|
|
109
|
+
connection: Connection;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
export interface IMessageContext {
|
|
113
|
+
roomId: string;
|
|
114
|
+
connection: Connection;
|
|
115
|
+
type: MessageType;
|
|
116
|
+
data: MessageData;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
export interface ICreateRoomContext {
|
|
120
|
+
roomId: string;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
export interface IEnterRoomContext {
|
|
124
|
+
roomId: string;
|
|
125
|
+
connection: Connection;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
export interface IDisconnectContext {
|
|
129
|
+
roomId: string;
|
|
130
|
+
connection: Connection;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
export interface ILeaveRoomContext {
|
|
134
|
+
roomId: string;
|
|
135
|
+
connection: Connection;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
export interface IDestroyRoomContext {
|
|
139
|
+
roomId: string;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
export interface IFeature {
|
|
143
|
+
middlewares?: IMiddlewares;
|
|
144
|
+
hooks?: IHooks;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* The collaboration connection of server.
|
|
149
|
+
*/
|
|
150
|
+
export declare class Connection {
|
|
151
|
+
/**
|
|
152
|
+
* Save custom data for each connection.
|
|
153
|
+
*/
|
|
154
|
+
tags: Map<string, unknown>;
|
|
155
|
+
/**
|
|
156
|
+
* Get the id of the connection.
|
|
157
|
+
* @returns {string} Returns the id of the connection.
|
|
158
|
+
*/
|
|
159
|
+
get id(): string;
|
|
160
|
+
/**
|
|
161
|
+
* The room id of the connection.
|
|
162
|
+
* @returns {string} Returns the room id of the connection.
|
|
163
|
+
*/
|
|
164
|
+
get roomId(): string;
|
|
165
|
+
/**
|
|
166
|
+
* Get the query arguments of the connection.
|
|
167
|
+
* @returns {Record<string, unknown>} Returns the query arguments of the connection.
|
|
168
|
+
*/
|
|
169
|
+
get query(): Record<string, unknown>;
|
|
170
|
+
/**
|
|
171
|
+
* Get the authentication data of the connection.
|
|
172
|
+
* @returns {[key: string]: any} Returns the authentication data of the connection.
|
|
173
|
+
*/
|
|
174
|
+
get auth(): {
|
|
175
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
176
|
+
[key: string]: any;
|
|
177
|
+
};
|
|
178
|
+
/**
|
|
179
|
+
* Send message to client
|
|
180
|
+
* @param data - message data to send
|
|
181
|
+
* @param type - message type
|
|
182
|
+
*/
|
|
183
|
+
send(data: MessageData, type?: MessageType): void;
|
|
184
|
+
/**
|
|
185
|
+
* Broadcast message to all connections in the room
|
|
186
|
+
* @param data - message data to send
|
|
187
|
+
* @param type - message type
|
|
188
|
+
* @param includeSelf - whether to send to self
|
|
189
|
+
*/
|
|
190
|
+
broadcast(data: string | Uint8Array, type: MessageType, includeSelf?: boolean): void;
|
|
191
|
+
/**
|
|
192
|
+
* Close the connection
|
|
193
|
+
*/
|
|
194
|
+
close(): void;
|
|
195
|
+
}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
!function(e,t){if("object"==typeof exports&&"object"==typeof module)module.exports=t();else if("function"==typeof define&&define.amd)define([],t);else{var o=t();for(var r in o)("object"==typeof exports?exports:e)[r]=o[r]}}(this,(()=>(()=>{"use strict";var e={42:(e,t,o)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.Connection=void 0,t.getConnection=function(e){return e.__conn__};const r=o(884);class n extends r.Observable{constructor(e){super(),this.tags=new Map,this.innerTags=new Map;const t=e.handshake.query;this._roomId=t.roomId,this._query=t,this._socket=e,this._socket.__conn__=this,this._socket.on(r.SOCKET_IO_MESSAGE_EVENT,this.onmessage.bind(this))}get id(){return this._socket.id}get roomId(){return this._roomId}get query(){return this._query}get auth(){return this._socket.handshake.auth}get socket(){return this._socket}send(e,t){this._socket.emit(r.SOCKET_IO_MESSAGE_EVENT,(0,r.encodeMessage)(e,t))}close(){this._socket.disconnect()}broadcast(e,t,o=!1){const n=(0,r.encodeMessage)(e,t);o&&this._socket.emit(r.SOCKET_IO_MESSAGE_EVENT,n),this._socket.to(this.roomId).emit(r.SOCKET_IO_MESSAGE_EVENT,n)}onmessage(e){this.emit("message",(0,r.decodeMessage)(e))}}t.Connection=n},273:(e,t)=>{Object.defineProperty(t,"__esModule",{value:!0})},897:(e,t)=>{Object.defineProperty(t,"__esModule",{value:!0})},271:function(e,t,o){var r=this&&this.__createBinding||(Object.create?function(e,t,o,r){void 0===r&&(r=o);var n=Object.getOwnPropertyDescriptor(t,o);n&&!("get"in n?!t.__esModule:n.writable||n.configurable)||(n={enumerable:!0,get:function(){return t[o]}}),Object.defineProperty(e,r,n)}:function(e,t,o,r){void 0===r&&(r=o),e[r]=t[o]}),n=this&&this.__setModuleDefault||(Object.create?function(e,t){Object.defineProperty(e,"default",{enumerable:!0,value:t})}:function(e,t){e.default=t}),s=this&&this.__importStar||function(e){if(e&&e.__esModule)return e;var t={};if(null!=e)for(var o in e)"default"!==o&&Object.prototype.hasOwnProperty.call(e,o)&&r(t,e,o);return n(t,e),t};Object.defineProperty(t,"__esModule",{value:!0}),t.Server=void 0;const i=s(o(952)),c=o(884),a=o(42);class u extends c.Middleware{constructor(e){var t;super(),this.rooms=new Set,e=null!=e?e:{port:3e3},this.io=new i.Server(e.httpServer||e.port,{path:null!==(t=e.path)&&void 0!==t?t:c.DEFAULT_REQUEST_PATH,maxHttpBufferSize:1e10,perMessageDeflate:{threshold:1024}}),this.io.use(((e,t)=>{const o=new a.Connection(e),r={connection:o,roomId:o.roomId};this.trigger("connect",r).then((()=>t())).catch((e=>t((e=>e instanceof Error?e:new Error(e))(e))))}));const o=this.io.of("/").adapter;this.io.on("connection",(e=>{const t=(0,a.getConnection)(e);this.emit("connect",{connection:t,roomId:t.roomId}),this.rooms.add(t.roomId),t.socket.join(t.roomId),t.on("message",((e,o)=>{this.trigger("message",{connection:t,data:e,type:o,roomId:t.roomId}).then((()=>this.emit("message",{connection:t,data:e,type:o,roomId:t.roomId}))).catch((e=>t.socket.emit("error",e)))}))})),o.on("create-room",(e=>{this.rooms.has(e)&&this.emit("createRoom",{roomId:e})})),o.on("join-room",((e,t)=>{if(!this.rooms.has(e))return;const o=this.io.sockets.sockets.get(t);this.emit("enterRoom",{roomId:e,connection:(0,a.getConnection)(o)})})),o.on("leave-room",((e,t)=>{if(!this.rooms.has(e))return;const o=this.io.sockets.sockets.get(t);this.emit("leaveRoom",{roomId:e,connection:(0,a.getConnection)(o)})})),o.on("delete-room",(e=>{this.rooms.has(e)&&(this.rooms.delete(e),this.emit("destroyRoom",{roomId:e}))}))}use(e,t){if("string"==typeof e){const o=e;super.use(o,t)}else{const t=e;t.connect&&super.use("connect",t.connect),t.message&&super.use("message",t.message)}}on(e,t){if("string"==typeof e){const o=e;super.on(o,t)}else{const t=e;t.connect&&super.on("connect",t.connect),t.createRoom&&super.on("createRoom",t.createRoom),t.enterRoom&&super.on("enterRoom",t.enterRoom),t.message&&super.on("message",t.message),t.disconnect&&super.on("disconnect",t.disconnect),t.leaveRoom&&super.on("leaveRoom",t.leaveRoom),t.destroyRoom&&super.on("destroyRoom",t.destroyRoom)}}useFeature(e){e.middlewares&&this.use(e.middlewares),e.hooks&&this.on(e.hooks)}}t.Server=u},871:e=>{e.exports=require("buffer")},331:e=>{e.exports=require("pako")},952:e=>{e.exports=require("socket.io")},884:function(e,t,o){var r=this&&this.__createBinding||(Object.create?function(e,t,o,r){void 0===r&&(r=o);var n=Object.getOwnPropertyDescriptor(t,o);n&&!("get"in n?!t.__esModule:n.writable||n.configurable)||(n={enumerable:!0,get:function(){return t[o]}}),Object.defineProperty(e,r,n)}:function(e,t,o,r){void 0===r&&(r=o),e[r]=t[o]}),n=this&&this.__exportStar||function(e,t){for(var o in e)"default"===o||Object.prototype.hasOwnProperty.call(t,o)||r(t,e,o)};Object.defineProperty(t,"__esModule",{value:!0}),t.DEFAULT_REQUEST_PATH=void 0,n(o(589),t),n(o(735),t),n(o(31),t),n(o(76),t),n(o(959),t),t.DEFAULT_REQUEST_PATH="/collaboration/"},31:(e,t)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.logger=void 0,t.logger=class{static info(e,...t){}static warn(e,...t){}static error(e,...t){}}},735:function(e,t,o){var r=this&&this.__createBinding||(Object.create?function(e,t,o,r){void 0===r&&(r=o);var n=Object.getOwnPropertyDescriptor(t,o);n&&!("get"in n?!t.__esModule:n.writable||n.configurable)||(n={enumerable:!0,get:function(){return t[o]}}),Object.defineProperty(e,r,n)}:function(e,t,o,r){void 0===r&&(r=o),e[r]=t[o]}),n=this&&this.__setModuleDefault||(Object.create?function(e,t){Object.defineProperty(e,"default",{enumerable:!0,value:t})}:function(e,t){e.default=t}),s=this&&this.__importStar||function(e){if(e&&e.__esModule)return e;var t={};if(null!=e)for(var o in e)"default"!==o&&Object.prototype.hasOwnProperty.call(e,o)&&r(t,e,o);return n(t,e),t};Object.defineProperty(t,"__esModule",{value:!0}),t.SOCKET_IO_MESSAGE_EVENT=void 0,t.encodeMessage=function(e,t=null){return[t,"string"==typeof e?a(e):e]},t.decodeMessage=function(e){return["string"==typeof e[1]?u(e[1]):e[1],e[0]]};const i=s(o(331)),c=o(871);function a(e){const t=(new TextEncoder).encode(e),o=i.deflate(t);return c.Buffer.from(o).toString("base64")}function u(e){const t=c.Buffer.from(e,"base64"),o=i.inflate(new Uint8Array(t));return(new TextDecoder).decode(o)}t.SOCKET_IO_MESSAGE_EVENT="m"},589:(e,t)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.Middleware=void 0,t.Middleware=class{constructor(){this.middlewares=new Map,this.hooks=new Map}use(e,t){this.middlewares.has(e)||this.middlewares.set(e,[]),this.middlewares.get(e).push(t)}on(e,t){this.hooks.has(e)||this.hooks.set(e,[]),this.hooks.get(e).push(t)}async trigger(e,t){let o=this.middlewares.get(e);if(!o)return;o=o.slice();const r=async e=>{if(e)throw e;const n=null==o?void 0:o.shift();n&&await n(t,r)};await r()}emit(e,t){const o=this.hooks.get(e);o&&o.forEach((e=>e(t)))}}},959:(e,t)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.Observable=void 0,t.Observable=class{constructor(){this._observers=new Map}on(e,t){return o(this._observers,e,(()=>new Set)).add(t),t}once(e,t){const o=(...r)=>{this.off(e,o),t(...r)};this.on(e,o)}off(e,t){const o=this._observers.get(e);void 0!==o&&(o.delete(t),0===o.size&&this._observers.delete(e))}emit(e,t){return Array.from((this._observers.get(e)||new Map).values()).forEach((e=>e(...t)))}destroy(){this._observers=new Map}};const o=(e,t,o)=>{let r=e.get(t);return void 0===r&&(r=o(),e.set(t,r)),r}},76:(e,t)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.isNullOrUndefined=function(e){return null==e}}},t={};function o(r){var n=t[r];if(void 0!==n)return n.exports;var s=t[r]={exports:{}};return e[r].call(s.exports,s,s.exports,o),s.exports}var r={};return(()=>{var e=r;Object.defineProperty(e,"__esModule",{value:!0}),e.Middleware=e.logger=e.Connection=e.Server=void 0,o(897),o(273);const t=o(271);Object.defineProperty(e,"Server",{enumerable:!0,get:function(){return t.Server}});const n=o(42);Object.defineProperty(e,"Connection",{enumerable:!0,get:function(){return n.Connection}});const s=o(884);Object.defineProperty(e,"logger",{enumerable:!0,get:function(){return s.logger}}),Object.defineProperty(e,"Middleware",{enumerable:!0,get:function(){return s.Middleware}})})(),r})()));
|
package/package.json
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@mescius/js-collaboration",
|
|
3
|
+
"version": "18.0.0",
|
|
4
|
+
"type": "commonjs",
|
|
5
|
+
"main": "./dist/index.js",
|
|
6
|
+
"exports": {
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"import": "./wrapper.mjs",
|
|
9
|
+
"require": "./dist/index.js"
|
|
10
|
+
},
|
|
11
|
+
"author": {
|
|
12
|
+
"email": "us.sales@mescius.com",
|
|
13
|
+
"name": "MESCIUS inc"
|
|
14
|
+
},
|
|
15
|
+
"license": "Commercial",
|
|
16
|
+
"description": "SpreadJS Collaboration plugin",
|
|
17
|
+
"dependencies": {
|
|
18
|
+
"socket.io": "4.8.1",
|
|
19
|
+
"buffer": "6.0.3",
|
|
20
|
+
"pako": "2.1.0"
|
|
21
|
+
},
|
|
22
|
+
"keywords": [
|
|
23
|
+
"js-collaboration",
|
|
24
|
+
"spread-sheets-collaboration",
|
|
25
|
+
"collaboration",
|
|
26
|
+
"spread",
|
|
27
|
+
"sheet",
|
|
28
|
+
"javascript",
|
|
29
|
+
"excel",
|
|
30
|
+
"spreadjs"
|
|
31
|
+
],
|
|
32
|
+
"homepage": "https://developer.mescius.com/spreadjs"
|
|
33
|
+
}
|
package/wrapper.mjs
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import * as collaborationModule from './dist/index.js';
|
|
2
|
+
import collaboration from './dist/index.js';
|
|
3
|
+
const { Server, Connection, logger, Middleware } = { ...(collaboration ?? {}), ...collaborationModule };
|
|
4
|
+
|
|
5
|
+
export { collaboration as default, Server, Connection, logger, Middleware };
|