@hocuspocus/extension-sqlite 1.0.0-alpha.5 → 1.0.0-alpha.6

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.
Files changed (72) hide show
  1. package/dist/hocuspocus-sqlite.cjs +27 -18
  2. package/dist/hocuspocus-sqlite.cjs.map +1 -1
  3. package/dist/hocuspocus-sqlite.esm.js +25 -19
  4. package/dist/hocuspocus-sqlite.esm.js.map +1 -1
  5. package/dist/packages/extension-database/src/Database.d.ts +5 -9
  6. package/dist/packages/extension-logger/src/Logger.d.ts +5 -0
  7. package/dist/packages/extension-sqlite/src/SQLite.d.ts +4 -4
  8. package/dist/packages/extension-webhook/src/index.d.ts +1 -2
  9. package/dist/packages/provider/src/EventEmitter.d.ts +1 -1
  10. package/dist/packages/provider/src/HocuspocusProvider.d.ts +7 -4
  11. package/dist/packages/provider/src/types.d.ts +10 -0
  12. package/dist/packages/server/src/Connection.d.ts +4 -4
  13. package/dist/packages/server/src/Debugger.d.ts +1 -2
  14. package/dist/packages/server/src/Document.d.ts +8 -5
  15. package/dist/packages/server/src/Hocuspocus.d.ts +21 -11
  16. package/dist/packages/server/src/MessageReceiver.d.ts +8 -5
  17. package/dist/packages/server/src/OutgoingMessage.d.ts +1 -0
  18. package/dist/packages/server/src/index.d.ts +5 -0
  19. package/dist/packages/server/src/types.d.ts +77 -15
  20. package/dist/tests/extension-logger/onListen.d.ts +1 -0
  21. package/dist/tests/extension-redis/onLoadDocument.d.ts +1 -0
  22. package/dist/tests/extension-redis/onSynced.d.ts +1 -0
  23. package/dist/tests/extension-redis-rewrite/closeConnections.d.ts +1 -0
  24. package/dist/tests/extension-redis-rewrite/getConnectionCount.d.ts +1 -0
  25. package/dist/tests/extension-redis-rewrite/getDocumentsCount.d.ts +1 -0
  26. package/dist/tests/extension-redis-rewrite/onAwarenessChange.d.ts +1 -0
  27. package/dist/tests/extension-redis-rewrite/onChange.d.ts +1 -0
  28. package/dist/tests/extension-redis-rewrite/onStoreDocument.d.ts +1 -0
  29. package/dist/tests/extension-rocksdb/onLoadDocument.d.ts +1 -0
  30. package/dist/tests/provider/configuration.d.ts +1 -0
  31. package/dist/tests/provider/onAuthenticated.d.ts +1 -0
  32. package/dist/tests/provider/onAuthenticationFailed.d.ts +1 -0
  33. package/dist/tests/provider/onAwarenessChange.d.ts +1 -0
  34. package/dist/tests/provider/onAwarenessUpdate.d.ts +1 -0
  35. package/dist/tests/provider/onClose.d.ts +1 -0
  36. package/dist/tests/provider/onConnect.d.ts +1 -0
  37. package/dist/tests/provider/onDisconnect.d.ts +1 -0
  38. package/dist/tests/provider/onMessage.d.ts +1 -0
  39. package/dist/tests/provider/onOpen.d.ts +1 -0
  40. package/dist/tests/provider/onSynced.d.ts +1 -0
  41. package/dist/tests/server/address.d.ts +1 -0
  42. package/dist/tests/server/afterStoreDocument.d.ts +1 -0
  43. package/dist/tests/server/closeConnections.d.ts +1 -0
  44. package/dist/tests/server/getConnectionsCount.d.ts +1 -0
  45. package/dist/tests/server/getDocumentName.d.ts +1 -0
  46. package/dist/tests/server/getDocumentsCount.d.ts +1 -0
  47. package/dist/tests/server/getMessageLogs.d.ts +1 -0
  48. package/dist/tests/server/listen.d.ts +1 -0
  49. package/dist/tests/server/onAuthenticate.d.ts +1 -0
  50. package/dist/tests/server/onAwarenessUpdate.d.ts +1 -0
  51. package/dist/tests/server/onChange.d.ts +1 -0
  52. package/dist/tests/server/onConfigure.d.ts +1 -0
  53. package/dist/tests/server/onConnect.d.ts +1 -0
  54. package/dist/tests/server/onDestroy.d.ts +1 -0
  55. package/dist/tests/server/onDisconnect.d.ts +1 -0
  56. package/dist/tests/server/onListen.d.ts +1 -0
  57. package/dist/tests/server/onLoadDocument.d.ts +1 -0
  58. package/dist/tests/server/onRequest.d.ts +1 -0
  59. package/dist/tests/server/onStoreDocument.d.ts +1 -0
  60. package/dist/tests/server/onUpgrade.d.ts +1 -0
  61. package/dist/tests/server/requiresAuthentication.d.ts +1 -0
  62. package/dist/tests/transformer/TiptapTransformer.d.ts +1 -0
  63. package/dist/tests/utils/createDirectory.d.ts +1 -0
  64. package/dist/tests/utils/flushRedis.d.ts +1 -0
  65. package/dist/tests/utils/index.d.ts +7 -0
  66. package/dist/tests/utils/newHocuspocus.d.ts +2 -0
  67. package/dist/tests/utils/newHocuspocusProvider.d.ts +3 -0
  68. package/dist/tests/utils/redisConnectionSettings.d.ts +4 -0
  69. package/dist/tests/utils/removeDirectory.d.ts +1 -0
  70. package/dist/tests/utils/sleep.d.ts +1 -0
  71. package/package.json +4 -4
  72. package/src/SQLite.ts +28 -20
@@ -11,36 +11,42 @@ function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'defau
11
11
  var sqlite3__default = /*#__PURE__*/_interopDefaultLegacy(sqlite3);
12
12
  var kleur__default = /*#__PURE__*/_interopDefaultLegacy(kleur);
13
13
 
14
+ const schema = `CREATE TABLE IF NOT EXISTS "documents" (
15
+ "name" varchar(255) NOT NULL,
16
+ "data" blob NOT NULL,
17
+ UNIQUE(name)
18
+ )`;
19
+ const selectQuery = `
20
+ SELECT data FROM "documents" WHERE name = $name ORDER BY rowid DESC
21
+ `;
22
+ const upsertQuery = `
23
+ INSERT INTO "documents" ("name", "data") VALUES ($name, $data)
24
+ ON CONFLICT(name) DO UPDATE SET data = $data
25
+ `;
14
26
  class SQLite extends extensionDatabase.Database {
15
- /**
16
- * Constructor
17
- */
18
27
  constructor(configuration) {
19
28
  super({});
20
29
  this.configuration = {
21
30
  database: ':memory:',
22
- schema: `CREATE TABLE IF NOT EXISTS "documents" (
23
- "name" varchar(255) NOT NULL,
24
- "data" blob NOT NULL
25
- );`,
26
- fetchUpdates: async ({ documentName }) => {
31
+ schema,
32
+ fetch: async ({ documentName }) => {
27
33
  return new Promise((resolve, reject) => {
28
34
  var _a;
29
- (_a = this.db) === null || _a === void 0 ? void 0 : _a.all('SELECT data FROM "documents" WHERE name = $name ORDER BY rowid', {
35
+ (_a = this.db) === null || _a === void 0 ? void 0 : _a.get(selectQuery, {
30
36
  $name: documentName,
31
- }, (error, rows) => {
37
+ }, (error, row) => {
32
38
  if (error) {
33
39
  reject(error);
34
40
  }
35
- resolve(rows.map(row => row.data));
41
+ resolve(row === null || row === void 0 ? void 0 : row.data);
36
42
  });
37
43
  });
38
44
  },
39
- storeUpdate: async ({ documentName, update }) => {
45
+ store: async ({ documentName, state }) => {
40
46
  var _a;
41
- (_a = this.db) === null || _a === void 0 ? void 0 : _a.run('INSERT INTO "documents" ("name", "data") VALUES ($name, $data)', {
47
+ (_a = this.db) === null || _a === void 0 ? void 0 : _a.run(upsertQuery, {
42
48
  $name: documentName,
43
- $data: update,
49
+ $data: state,
44
50
  });
45
51
  },
46
52
  };
@@ -49,15 +55,18 @@ class SQLite extends extensionDatabase.Database {
49
55
  ...configuration,
50
56
  };
51
57
  }
52
- async onListen() {
53
- console.warn(` ${kleur__default["default"].yellow('The SQLite extension is intended to be used in a local development environment, not in a production environment.')}`);
54
- console.log();
55
- }
56
58
  async onConfigure() {
57
59
  this.db = new sqlite3__default["default"].Database(this.configuration.database);
58
60
  this.db.run(this.configuration.schema);
59
61
  }
62
+ async onListen() {
63
+ console.warn(` ${kleur__default["default"].yellow('The SQLite extension is intended to be used in a local development environment, not in a production environment.')}`);
64
+ console.log();
65
+ }
60
66
  }
61
67
 
62
68
  exports.SQLite = SQLite;
69
+ exports.schema = schema;
70
+ exports.selectQuery = selectQuery;
71
+ exports.upsertQuery = upsertQuery;
63
72
  //# sourceMappingURL=hocuspocus-sqlite.cjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"hocuspocus-sqlite.cjs","sources":["../src/SQLite.ts"],"sourcesContent":["import { Database, DatabaseConfiguration } from '@hocuspocus/extension-database'\nimport sqlite3 from 'sqlite3'\nimport kleur from 'kleur'\n\nexport interface SQLiteConfiguration extends DatabaseConfiguration {\n /**\n * Valid values are filenames, \":memory:\" for an anonymous in-memory database and an empty\n * string for an anonymous disk-based database. Anonymous databases are not persisted and\n * when closing the database handle, their contents are lost.\n *\n * https://github.com/mapbox/node-sqlite3/wiki/API#new-sqlite3databasefilename-mode-callback\n */\n database: string,\n /**\n * The database schema to create.\n */\n schema: string,\n}\n\nexport class SQLite extends Database {\n db?: sqlite3.Database\n\n configuration: SQLiteConfiguration = {\n database: ':memory:',\n schema: `CREATE TABLE IF NOT EXISTS \"documents\" (\n \"name\" varchar(255) NOT NULL,\n \"data\" blob NOT NULL\n );`,\n fetchUpdates: async ({ documentName }) => {\n return new Promise((resolve, reject) => {\n this.db?.all('SELECT data FROM \"documents\" WHERE name = $name ORDER BY rowid', {\n $name: documentName,\n }, (error, rows) => {\n if (error) {\n reject(error)\n }\n\n resolve(rows.map(row => row.data))\n })\n })\n },\n storeUpdate: async ({ documentName, update }) => {\n this.db?.run('INSERT INTO \"documents\" (\"name\", \"data\") VALUES ($name, $data)', {\n $name: documentName,\n $data: update,\n })\n },\n }\n\n /**\n * Constructor\n */\n constructor(configuration?: Partial<SQLiteConfiguration>) {\n super({})\n\n this.configuration = {\n ...this.configuration,\n ...configuration,\n }\n }\n\n async onListen() {\n console.warn(` ${kleur.yellow('The SQLite extension is intended to be used in a local development environment, not in a production environment.')}`)\n console.log()\n }\n\n async onConfigure() {\n this.db = new sqlite3.Database(this.configuration.database)\n\n this.db.run(this.configuration.schema)\n }\n}\n"],"names":["Database","kleur","sqlite3"],"mappings":";;;;;;;;;;;;;MAmBa,MAAO,SAAQA,0BAAQ;;;;IAiClC,YAAY,aAA4C;QACtD,KAAK,CAAC,EAAE,CAAC,CAAA;QA/BX,kBAAa,GAAwB;YACnC,QAAQ,EAAE,UAAU;YACpB,MAAM,EAAE;;;OAGL;YACH,YAAY,EAAE,OAAO,EAAE,YAAY,EAAE;gBACnC,OAAO,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,MAAM;;oBACjC,MAAA,IAAI,CAAC,EAAE,0CAAE,GAAG,CAAC,gEAAgE,EAAE;wBAC7E,KAAK,EAAE,YAAY;qBACpB,EAAE,CAAC,KAAK,EAAE,IAAI;wBACb,IAAI,KAAK,EAAE;4BACT,MAAM,CAAC,KAAK,CAAC,CAAA;yBACd;wBAED,OAAO,CAAC,IAAI,CAAC,GAAG,CAAC,GAAG,IAAI,GAAG,CAAC,IAAI,CAAC,CAAC,CAAA;qBACnC,CAAC,CAAA;iBACH,CAAC,CAAA;aACH;YACD,WAAW,EAAE,OAAO,EAAE,YAAY,EAAE,MAAM,EAAE;;gBAC1C,MAAA,IAAI,CAAC,EAAE,0CAAE,GAAG,CAAC,gEAAgE,EAAE;oBAC7E,KAAK,EAAE,YAAY;oBACnB,KAAK,EAAE,MAAM;iBACd,CAAC,CAAA;aACH;SACF,CAAA;QAQC,IAAI,CAAC,aAAa,GAAG;YACnB,GAAG,IAAI,CAAC,aAAa;YACrB,GAAG,aAAa;SACjB,CAAA;KACF;IAED,MAAM,QAAQ;QACZ,OAAO,CAAC,IAAI,CAAC,KAAKC,yBAAK,CAAC,MAAM,CAAC,kHAAkH,CAAC,EAAE,CAAC,CAAA;QACrJ,OAAO,CAAC,GAAG,EAAE,CAAA;KACd;IAED,MAAM,WAAW;QACf,IAAI,CAAC,EAAE,GAAG,IAAIC,2BAAO,CAAC,QAAQ,CAAC,IAAI,CAAC,aAAa,CAAC,QAAQ,CAAC,CAAA;QAE3D,IAAI,CAAC,EAAE,CAAC,GAAG,CAAC,IAAI,CAAC,aAAa,CAAC,MAAM,CAAC,CAAA;KACvC;;;;;"}
1
+ {"version":3,"file":"hocuspocus-sqlite.cjs","sources":["../src/SQLite.ts"],"sourcesContent":["import { Database, DatabaseConfiguration } from '@hocuspocus/extension-database'\nimport sqlite3 from 'sqlite3'\nimport kleur from 'kleur'\n\nexport const schema = `CREATE TABLE IF NOT EXISTS \"documents\" (\n \"name\" varchar(255) NOT NULL,\n \"data\" blob NOT NULL,\n UNIQUE(name)\n)`\n\nexport const selectQuery = `\n SELECT data FROM \"documents\" WHERE name = $name ORDER BY rowid DESC\n`\n\nexport const upsertQuery = `\n INSERT INTO \"documents\" (\"name\", \"data\") VALUES ($name, $data)\n ON CONFLICT(name) DO UPDATE SET data = $data\n`\n\nexport interface SQLiteConfiguration extends DatabaseConfiguration {\n /**\n * Valid values are filenames, \":memory:\" for an anonymous in-memory database and an empty\n * string for an anonymous disk-based database. Anonymous databases are not persisted and\n * when closing the database handle, their contents are lost.\n *\n * https://github.com/mapbox/node-sqlite3/wiki/API#new-sqlite3databasefilename-mode-callback\n */\n database: string,\n /**\n * The database schema to create.\n */\n schema: string,\n}\n\nexport class SQLite extends Database {\n db?: sqlite3.Database\n\n configuration: SQLiteConfiguration = {\n database: ':memory:',\n schema,\n fetch: async ({ documentName }) => {\n return new Promise((resolve, reject) => {\n this.db?.get(selectQuery, {\n $name: documentName,\n }, (error, row) => {\n if (error) {\n reject(error)\n }\n\n resolve(row?.data)\n })\n })\n },\n store: async ({ documentName, state }) => {\n this.db?.run(upsertQuery, {\n $name: documentName,\n $data: state,\n })\n },\n }\n\n constructor(configuration?: Partial<SQLiteConfiguration>) {\n super({})\n\n this.configuration = {\n ...this.configuration,\n ...configuration,\n }\n }\n\n async onConfigure() {\n this.db = new sqlite3.Database(this.configuration.database)\n this.db.run(this.configuration.schema)\n }\n\n async onListen() {\n console.warn(` ${kleur.yellow('The SQLite extension is intended to be used in a local development environment, not in a production environment.')}`)\n console.log()\n }\n}\n"],"names":["Database","sqlite3","kleur"],"mappings":";;;;;;;;;;;;;MAIa,MAAM,GAAG;;;;GAIpB;MAEW,WAAW,GAAG;;EAE1B;MAEY,WAAW,GAAG;;;EAG1B;MAiBY,MAAO,SAAQA,0BAAQ;IA2BlC,YAAY,aAA4C;QACtD,KAAK,CAAC,EAAE,CAAC,CAAA;QAzBX,kBAAa,GAAwB;YACnC,QAAQ,EAAE,UAAU;YACpB,MAAM;YACN,KAAK,EAAE,OAAO,EAAE,YAAY,EAAE;gBAC5B,OAAO,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,MAAM;;oBACjC,MAAA,IAAI,CAAC,EAAE,0CAAE,GAAG,CAAC,WAAW,EAAE;wBACxB,KAAK,EAAE,YAAY;qBACpB,EAAE,CAAC,KAAK,EAAE,GAAG;wBACZ,IAAI,KAAK,EAAE;4BACT,MAAM,CAAC,KAAK,CAAC,CAAA;yBACd;wBAED,OAAO,CAAC,GAAG,aAAH,GAAG,uBAAH,GAAG,CAAE,IAAI,CAAC,CAAA;qBACnB,CAAC,CAAA;iBACH,CAAC,CAAA;aACH;YACD,KAAK,EAAE,OAAO,EAAE,YAAY,EAAE,KAAK,EAAE;;gBACnC,MAAA,IAAI,CAAC,EAAE,0CAAE,GAAG,CAAC,WAAW,EAAE;oBACxB,KAAK,EAAE,YAAY;oBACnB,KAAK,EAAE,KAAK;iBACb,CAAC,CAAA;aACH;SACF,CAAA;QAKC,IAAI,CAAC,aAAa,GAAG;YACnB,GAAG,IAAI,CAAC,aAAa;YACrB,GAAG,aAAa;SACjB,CAAA;KACF;IAED,MAAM,WAAW;QACf,IAAI,CAAC,EAAE,GAAG,IAAIC,2BAAO,CAAC,QAAQ,CAAC,IAAI,CAAC,aAAa,CAAC,QAAQ,CAAC,CAAA;QAC3D,IAAI,CAAC,EAAE,CAAC,GAAG,CAAC,IAAI,CAAC,aAAa,CAAC,MAAM,CAAC,CAAA;KACvC;IAED,MAAM,QAAQ;QACZ,OAAO,CAAC,IAAI,CAAC,KAAKC,yBAAK,CAAC,MAAM,CAAC,kHAAkH,CAAC,EAAE,CAAC,CAAA;QACrJ,OAAO,CAAC,GAAG,EAAE,CAAA;KACd;;;;;;;;"}
@@ -2,36 +2,42 @@ import { Database } from '@hocuspocus/extension-database';
2
2
  import sqlite3 from 'sqlite3';
3
3
  import kleur from 'kleur';
4
4
 
5
+ const schema = `CREATE TABLE IF NOT EXISTS "documents" (
6
+ "name" varchar(255) NOT NULL,
7
+ "data" blob NOT NULL,
8
+ UNIQUE(name)
9
+ )`;
10
+ const selectQuery = `
11
+ SELECT data FROM "documents" WHERE name = $name ORDER BY rowid DESC
12
+ `;
13
+ const upsertQuery = `
14
+ INSERT INTO "documents" ("name", "data") VALUES ($name, $data)
15
+ ON CONFLICT(name) DO UPDATE SET data = $data
16
+ `;
5
17
  class SQLite extends Database {
6
- /**
7
- * Constructor
8
- */
9
18
  constructor(configuration) {
10
19
  super({});
11
20
  this.configuration = {
12
21
  database: ':memory:',
13
- schema: `CREATE TABLE IF NOT EXISTS "documents" (
14
- "name" varchar(255) NOT NULL,
15
- "data" blob NOT NULL
16
- );`,
17
- fetchUpdates: async ({ documentName }) => {
22
+ schema,
23
+ fetch: async ({ documentName }) => {
18
24
  return new Promise((resolve, reject) => {
19
25
  var _a;
20
- (_a = this.db) === null || _a === void 0 ? void 0 : _a.all('SELECT data FROM "documents" WHERE name = $name ORDER BY rowid', {
26
+ (_a = this.db) === null || _a === void 0 ? void 0 : _a.get(selectQuery, {
21
27
  $name: documentName,
22
- }, (error, rows) => {
28
+ }, (error, row) => {
23
29
  if (error) {
24
30
  reject(error);
25
31
  }
26
- resolve(rows.map(row => row.data));
32
+ resolve(row === null || row === void 0 ? void 0 : row.data);
27
33
  });
28
34
  });
29
35
  },
30
- storeUpdate: async ({ documentName, update }) => {
36
+ store: async ({ documentName, state }) => {
31
37
  var _a;
32
- (_a = this.db) === null || _a === void 0 ? void 0 : _a.run('INSERT INTO "documents" ("name", "data") VALUES ($name, $data)', {
38
+ (_a = this.db) === null || _a === void 0 ? void 0 : _a.run(upsertQuery, {
33
39
  $name: documentName,
34
- $data: update,
40
+ $data: state,
35
41
  });
36
42
  },
37
43
  };
@@ -40,15 +46,15 @@ class SQLite extends Database {
40
46
  ...configuration,
41
47
  };
42
48
  }
43
- async onListen() {
44
- console.warn(` ${kleur.yellow('The SQLite extension is intended to be used in a local development environment, not in a production environment.')}`);
45
- console.log();
46
- }
47
49
  async onConfigure() {
48
50
  this.db = new sqlite3.Database(this.configuration.database);
49
51
  this.db.run(this.configuration.schema);
50
52
  }
53
+ async onListen() {
54
+ console.warn(` ${kleur.yellow('The SQLite extension is intended to be used in a local development environment, not in a production environment.')}`);
55
+ console.log();
56
+ }
51
57
  }
52
58
 
53
- export { SQLite };
59
+ export { SQLite, schema, selectQuery, upsertQuery };
54
60
  //# sourceMappingURL=hocuspocus-sqlite.esm.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"hocuspocus-sqlite.esm.js","sources":["../src/SQLite.ts"],"sourcesContent":["import { Database, DatabaseConfiguration } from '@hocuspocus/extension-database'\nimport sqlite3 from 'sqlite3'\nimport kleur from 'kleur'\n\nexport interface SQLiteConfiguration extends DatabaseConfiguration {\n /**\n * Valid values are filenames, \":memory:\" for an anonymous in-memory database and an empty\n * string for an anonymous disk-based database. Anonymous databases are not persisted and\n * when closing the database handle, their contents are lost.\n *\n * https://github.com/mapbox/node-sqlite3/wiki/API#new-sqlite3databasefilename-mode-callback\n */\n database: string,\n /**\n * The database schema to create.\n */\n schema: string,\n}\n\nexport class SQLite extends Database {\n db?: sqlite3.Database\n\n configuration: SQLiteConfiguration = {\n database: ':memory:',\n schema: `CREATE TABLE IF NOT EXISTS \"documents\" (\n \"name\" varchar(255) NOT NULL,\n \"data\" blob NOT NULL\n );`,\n fetchUpdates: async ({ documentName }) => {\n return new Promise((resolve, reject) => {\n this.db?.all('SELECT data FROM \"documents\" WHERE name = $name ORDER BY rowid', {\n $name: documentName,\n }, (error, rows) => {\n if (error) {\n reject(error)\n }\n\n resolve(rows.map(row => row.data))\n })\n })\n },\n storeUpdate: async ({ documentName, update }) => {\n this.db?.run('INSERT INTO \"documents\" (\"name\", \"data\") VALUES ($name, $data)', {\n $name: documentName,\n $data: update,\n })\n },\n }\n\n /**\n * Constructor\n */\n constructor(configuration?: Partial<SQLiteConfiguration>) {\n super({})\n\n this.configuration = {\n ...this.configuration,\n ...configuration,\n }\n }\n\n async onListen() {\n console.warn(` ${kleur.yellow('The SQLite extension is intended to be used in a local development environment, not in a production environment.')}`)\n console.log()\n }\n\n async onConfigure() {\n this.db = new sqlite3.Database(this.configuration.database)\n\n this.db.run(this.configuration.schema)\n }\n}\n"],"names":[],"mappings":";;;;MAmBa,MAAO,SAAQ,QAAQ;;;;IAiClC,YAAY,aAA4C;QACtD,KAAK,CAAC,EAAE,CAAC,CAAA;QA/BX,kBAAa,GAAwB;YACnC,QAAQ,EAAE,UAAU;YACpB,MAAM,EAAE;;;OAGL;YACH,YAAY,EAAE,OAAO,EAAE,YAAY,EAAE;gBACnC,OAAO,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,MAAM;;oBACjC,MAAA,IAAI,CAAC,EAAE,0CAAE,GAAG,CAAC,gEAAgE,EAAE;wBAC7E,KAAK,EAAE,YAAY;qBACpB,EAAE,CAAC,KAAK,EAAE,IAAI;wBACb,IAAI,KAAK,EAAE;4BACT,MAAM,CAAC,KAAK,CAAC,CAAA;yBACd;wBAED,OAAO,CAAC,IAAI,CAAC,GAAG,CAAC,GAAG,IAAI,GAAG,CAAC,IAAI,CAAC,CAAC,CAAA;qBACnC,CAAC,CAAA;iBACH,CAAC,CAAA;aACH;YACD,WAAW,EAAE,OAAO,EAAE,YAAY,EAAE,MAAM,EAAE;;gBAC1C,MAAA,IAAI,CAAC,EAAE,0CAAE,GAAG,CAAC,gEAAgE,EAAE;oBAC7E,KAAK,EAAE,YAAY;oBACnB,KAAK,EAAE,MAAM;iBACd,CAAC,CAAA;aACH;SACF,CAAA;QAQC,IAAI,CAAC,aAAa,GAAG;YACnB,GAAG,IAAI,CAAC,aAAa;YACrB,GAAG,aAAa;SACjB,CAAA;KACF;IAED,MAAM,QAAQ;QACZ,OAAO,CAAC,IAAI,CAAC,KAAK,KAAK,CAAC,MAAM,CAAC,kHAAkH,CAAC,EAAE,CAAC,CAAA;QACrJ,OAAO,CAAC,GAAG,EAAE,CAAA;KACd;IAED,MAAM,WAAW;QACf,IAAI,CAAC,EAAE,GAAG,IAAI,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAC,aAAa,CAAC,QAAQ,CAAC,CAAA;QAE3D,IAAI,CAAC,EAAE,CAAC,GAAG,CAAC,IAAI,CAAC,aAAa,CAAC,MAAM,CAAC,CAAA;KACvC;;;;;"}
1
+ {"version":3,"file":"hocuspocus-sqlite.esm.js","sources":["../src/SQLite.ts"],"sourcesContent":["import { Database, DatabaseConfiguration } from '@hocuspocus/extension-database'\nimport sqlite3 from 'sqlite3'\nimport kleur from 'kleur'\n\nexport const schema = `CREATE TABLE IF NOT EXISTS \"documents\" (\n \"name\" varchar(255) NOT NULL,\n \"data\" blob NOT NULL,\n UNIQUE(name)\n)`\n\nexport const selectQuery = `\n SELECT data FROM \"documents\" WHERE name = $name ORDER BY rowid DESC\n`\n\nexport const upsertQuery = `\n INSERT INTO \"documents\" (\"name\", \"data\") VALUES ($name, $data)\n ON CONFLICT(name) DO UPDATE SET data = $data\n`\n\nexport interface SQLiteConfiguration extends DatabaseConfiguration {\n /**\n * Valid values are filenames, \":memory:\" for an anonymous in-memory database and an empty\n * string for an anonymous disk-based database. Anonymous databases are not persisted and\n * when closing the database handle, their contents are lost.\n *\n * https://github.com/mapbox/node-sqlite3/wiki/API#new-sqlite3databasefilename-mode-callback\n */\n database: string,\n /**\n * The database schema to create.\n */\n schema: string,\n}\n\nexport class SQLite extends Database {\n db?: sqlite3.Database\n\n configuration: SQLiteConfiguration = {\n database: ':memory:',\n schema,\n fetch: async ({ documentName }) => {\n return new Promise((resolve, reject) => {\n this.db?.get(selectQuery, {\n $name: documentName,\n }, (error, row) => {\n if (error) {\n reject(error)\n }\n\n resolve(row?.data)\n })\n })\n },\n store: async ({ documentName, state }) => {\n this.db?.run(upsertQuery, {\n $name: documentName,\n $data: state,\n })\n },\n }\n\n constructor(configuration?: Partial<SQLiteConfiguration>) {\n super({})\n\n this.configuration = {\n ...this.configuration,\n ...configuration,\n }\n }\n\n async onConfigure() {\n this.db = new sqlite3.Database(this.configuration.database)\n this.db.run(this.configuration.schema)\n }\n\n async onListen() {\n console.warn(` ${kleur.yellow('The SQLite extension is intended to be used in a local development environment, not in a production environment.')}`)\n console.log()\n }\n}\n"],"names":[],"mappings":";;;;MAIa,MAAM,GAAG;;;;GAIpB;MAEW,WAAW,GAAG;;EAE1B;MAEY,WAAW,GAAG;;;EAG1B;MAiBY,MAAO,SAAQ,QAAQ;IA2BlC,YAAY,aAA4C;QACtD,KAAK,CAAC,EAAE,CAAC,CAAA;QAzBX,kBAAa,GAAwB;YACnC,QAAQ,EAAE,UAAU;YACpB,MAAM;YACN,KAAK,EAAE,OAAO,EAAE,YAAY,EAAE;gBAC5B,OAAO,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,MAAM;;oBACjC,MAAA,IAAI,CAAC,EAAE,0CAAE,GAAG,CAAC,WAAW,EAAE;wBACxB,KAAK,EAAE,YAAY;qBACpB,EAAE,CAAC,KAAK,EAAE,GAAG;wBACZ,IAAI,KAAK,EAAE;4BACT,MAAM,CAAC,KAAK,CAAC,CAAA;yBACd;wBAED,OAAO,CAAC,GAAG,aAAH,GAAG,uBAAH,GAAG,CAAE,IAAI,CAAC,CAAA;qBACnB,CAAC,CAAA;iBACH,CAAC,CAAA;aACH;YACD,KAAK,EAAE,OAAO,EAAE,YAAY,EAAE,KAAK,EAAE;;gBACnC,MAAA,IAAI,CAAC,EAAE,0CAAE,GAAG,CAAC,WAAW,EAAE;oBACxB,KAAK,EAAE,YAAY;oBACnB,KAAK,EAAE,KAAK;iBACb,CAAC,CAAA;aACH;SACF,CAAA;QAKC,IAAI,CAAC,aAAa,GAAG;YACnB,GAAG,IAAI,CAAC,aAAa;YACrB,GAAG,aAAa;SACjB,CAAA;KACF;IAED,MAAM,WAAW;QACf,IAAI,CAAC,EAAE,GAAG,IAAI,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAC,aAAa,CAAC,QAAQ,CAAC,CAAA;QAC3D,IAAI,CAAC,EAAE,CAAC,GAAG,CAAC,IAAI,CAAC,aAAa,CAAC,MAAM,CAAC,CAAA;KACvC;IAED,MAAM,QAAQ;QACZ,OAAO,CAAC,IAAI,CAAC,KAAK,KAAK,CAAC,MAAM,CAAC,kHAAkH,CAAC,EAAE,CAAC,CAAA;QACrJ,OAAO,CAAC,GAAG,EAAE,CAAA;KACd;;;;;"}
@@ -1,20 +1,16 @@
1
- /// <reference types="node" />
2
- import { Extension, onChangePayload, onLoadDocumentPayload } from '@hocuspocus/server';
1
+ import { Extension, onChangePayload, onLoadDocumentPayload, storePayload } from '@hocuspocus/server';
3
2
  export interface DatabaseConfiguration {
4
3
  /**
5
4
  * Pass a Promise to retrieve updates from your database. The Promise should resolve to
6
5
  * an array of items with Y.js-compatible binary data.
7
6
  */
8
- fetchUpdates: ({ documentName }: {
7
+ fetch: ({ documentName }: {
9
8
  documentName: string;
10
- }) => Promise<Uint8Array[]>;
9
+ }) => Promise<Uint8Array | null>;
11
10
  /**
12
11
  * Pass a function to store updates in your database.
13
12
  */
14
- storeUpdate: ({ update, documentName }: {
15
- update: Buffer;
16
- documentName: string;
17
- }) => void;
13
+ store: (data: storePayload) => void;
18
14
  }
19
15
  export declare class Database implements Extension {
20
16
  /**
@@ -32,5 +28,5 @@ export declare class Database implements Extension {
32
28
  /**
33
29
  * Store new updates in the database.
34
30
  */
35
- onChange({ document, documentName }: onChangePayload): Promise<void>;
31
+ onStoreDocument(data: onChangePayload): Promise<void>;
36
32
  }
@@ -14,6 +14,10 @@ export interface LoggerConfiguration {
14
14
  * Whether to log something for the `onChange` hook.
15
15
  */
16
16
  onChange: boolean;
17
+ /**
18
+ * Whether to log something for the `onStoreDocument` hook.
19
+ */
20
+ onStoreDocument: boolean;
17
21
  /**
18
22
  * Whether to log something for the `onConnect` hook.
19
23
  */
@@ -53,6 +57,7 @@ export declare class Logger implements Extension {
53
57
  onConfigure(data: onConfigurePayload): Promise<void>;
54
58
  onLoadDocument(data: onLoadDocumentPayload): Promise<void>;
55
59
  onChange(data: onChangePayload): Promise<void>;
60
+ onStoreDocument(data: onDisconnectPayload): Promise<void>;
56
61
  onConnect(data: onConnectPayload): Promise<void>;
57
62
  onDisconnect(data: onDisconnectPayload): Promise<void>;
58
63
  onUpgrade(data: onUpgradePayload): Promise<void>;
@@ -1,5 +1,8 @@
1
1
  import { Database, DatabaseConfiguration } from '@hocuspocus/extension-database';
2
2
  import sqlite3 from 'sqlite3';
3
+ 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)";
4
+ export declare const selectQuery = "\n SELECT data FROM \"documents\" WHERE name = $name ORDER BY rowid DESC\n";
5
+ export declare const upsertQuery = "\n INSERT INTO \"documents\" (\"name\", \"data\") VALUES ($name, $data)\n ON CONFLICT(name) DO UPDATE SET data = $data\n";
3
6
  export interface SQLiteConfiguration extends DatabaseConfiguration {
4
7
  /**
5
8
  * Valid values are filenames, ":memory:" for an anonymous in-memory database and an empty
@@ -17,10 +20,7 @@ export interface SQLiteConfiguration extends DatabaseConfiguration {
17
20
  export declare class SQLite extends Database {
18
21
  db?: sqlite3.Database;
19
22
  configuration: SQLiteConfiguration;
20
- /**
21
- * Constructor
22
- */
23
23
  constructor(configuration?: Partial<SQLiteConfiguration>);
24
- onListen(): Promise<void>;
25
24
  onConfigure(): Promise<void>;
25
+ onListen(): Promise<void>;
26
26
  }
@@ -3,7 +3,6 @@ import { Extension, onChangePayload, onConnectPayload, onLoadDocumentPayload, on
3
3
  import { Doc } from 'yjs';
4
4
  import { Transformer } from '@hocuspocus/transformer';
5
5
  import { AxiosResponse } from 'axios';
6
- import Timeout = NodeJS.Timeout;
7
6
  export declare enum Events {
8
7
  onChange = "change",
9
8
  onConnect = "connect",
@@ -24,7 +23,7 @@ export interface Configuration {
24
23
  export declare class Webhook implements Extension {
25
24
  configuration: Configuration;
26
25
  debounced: Map<string, {
27
- timeout: Timeout;
26
+ timeout: NodeJS.Timeout;
28
27
  start: number;
29
28
  }>;
30
29
  /**
@@ -5,5 +5,5 @@ export default class EventEmitter {
5
5
  on(event: string, fn: Function): this;
6
6
  protected emit(event: string, ...args: any): this;
7
7
  off(event: string, fn?: Function): this;
8
- protected removeAllListeners(): void;
8
+ removeAllListeners(): void;
9
9
  }
@@ -1,10 +1,11 @@
1
1
  import * as Y from 'yjs';
2
2
  import { Awareness } from 'y-protocols/awareness';
3
3
  import * as mutex from 'lib0/mutex';
4
- import { Event, CloseEvent, MessageEvent } from 'ws';
4
+ import type { Event, CloseEvent, MessageEvent } from 'ws';
5
5
  import EventEmitter from './EventEmitter';
6
6
  import { OutgoingMessage } from './OutgoingMessage';
7
7
  import { ConstructableOutgoingMessage } from './types';
8
+ import { onAwarenessChangeParameters, onAwarenessUpdateParameters } from '.';
8
9
  export declare enum WebSocketStatus {
9
10
  Connecting = "connecting",
10
11
  Connected = "connected",
@@ -99,12 +100,14 @@ export interface CompleteHocuspocusProviderConfiguration {
99
100
  onMessage: (event: MessageEvent) => void;
100
101
  onOutgoingMessage: (message: OutgoingMessage) => void;
101
102
  onStatus: (status: any) => void;
102
- onSynced: () => void;
103
+ onSynced: ({ state }: {
104
+ state: boolean;
105
+ }) => void;
103
106
  onDisconnect: (event: CloseEvent) => void;
104
107
  onClose: (event: CloseEvent) => void;
105
108
  onDestroy: () => void;
106
- onAwarenessUpdate: (states: any) => void;
107
- onAwarenessChange: (states: any) => void;
109
+ onAwarenessUpdate: ({ states }: onAwarenessUpdateParameters) => void;
110
+ onAwarenessChange: ({ states }: onAwarenessChangeParameters) => void;
108
111
  /**
109
112
  * Don’t output any warnings.
110
113
  */
@@ -32,3 +32,13 @@ export interface Constructable<T> {
32
32
  new (...args: any): T;
33
33
  }
34
34
  export declare type ConstructableOutgoingMessage = Constructable<AuthenticationMessage> | Constructable<AwarenessMessage> | Constructable<QueryAwarenessMessage> | Constructable<SyncStepOneMessage> | Constructable<SyncStepTwoMessage> | Constructable<UpdateMessage>;
35
+ export declare type onAwarenessUpdateParameters = {
36
+ states: StatesArray;
37
+ };
38
+ export declare type onAwarenessChangeParameters = {
39
+ states: StatesArray;
40
+ };
41
+ export declare type StatesArray = {
42
+ clientId: number;
43
+ [key: string | number]: any;
44
+ }[];
@@ -4,8 +4,8 @@ import WebSocket from 'ws';
4
4
  import { IncomingMessage as HTTPIncomingMessage } from 'http';
5
5
  import { CloseEvent } from '@hocuspocus/common';
6
6
  import Document from './Document';
7
- import { MessageLogger } from './Debugger';
8
- declare class Connection {
7
+ import { Debugger } from './Debugger';
8
+ export declare class Connection {
9
9
  webSocket: WebSocket;
10
10
  context: any;
11
11
  document: Document;
@@ -17,11 +17,11 @@ declare class Connection {
17
17
  socketId: string;
18
18
  lock: AsyncLock;
19
19
  readOnly: Boolean;
20
- debugger: MessageLogger;
20
+ logger: Debugger;
21
21
  /**
22
22
  * Constructor.
23
23
  */
24
- constructor(connection: WebSocket, request: HTTPIncomingMessage, document: Document, timeout: number, socketId: string, context: any, readOnly?: boolean);
24
+ constructor(connection: WebSocket, request: HTTPIncomingMessage, document: Document, timeout: number, socketId: string, context: any, readOnly: boolean | undefined, logger: Debugger);
25
25
  /**
26
26
  * Set a callback that will be triggered when the connection is closed
27
27
  */
@@ -1,4 +1,4 @@
1
- export declare class MessageLogger {
1
+ export declare class Debugger {
2
2
  logs: any[];
3
3
  listen: boolean;
4
4
  output: boolean;
@@ -12,4 +12,3 @@ export declare class MessageLogger {
12
12
  logs: any[];
13
13
  };
14
14
  }
15
- export declare const Debugger: MessageLogger;
@@ -3,20 +3,23 @@ import { Awareness } from 'y-protocols/awareness';
3
3
  import { Doc } from 'yjs';
4
4
  import { mutex } from 'lib0/mutex.js';
5
5
  import Connection from './Connection';
6
- import { MessageLogger } from './Debugger';
7
- declare class Document extends Doc {
6
+ import { Debugger } from './Debugger';
7
+ export declare class Document extends Doc {
8
8
  awareness: Awareness;
9
9
  callbacks: {
10
10
  onUpdate: (document: Document, connection: Connection, update: Uint8Array) => void;
11
11
  };
12
- connections: Map<any, any>;
12
+ connections: Map<WebSocket, {
13
+ clients: Set<any>;
14
+ connection: Connection;
15
+ }>;
13
16
  name: string;
14
17
  mux: mutex;
15
- debugger: MessageLogger;
18
+ logger: Debugger;
16
19
  /**
17
20
  * Constructor.
18
21
  */
19
- constructor(name: string);
22
+ constructor(name: string, logger: Debugger);
20
23
  /**
21
24
  * Check if the Document is empty
22
25
  */
@@ -1,14 +1,16 @@
1
1
  /// <reference types="node" />
2
- import WebSocket, { WebSocketServer } from 'ws';
2
+ import WebSocket, { AddressInfo, WebSocketServer } from 'ws';
3
3
  import { IncomingMessage, Server as HTTPServer } from 'http';
4
4
  import { Configuration, Hook } from './types';
5
5
  import Document from './Document';
6
- import { MessageLogger } from './Debugger';
6
+ import { Debugger } from './Debugger';
7
7
  import { onListenPayload } from '.';
8
8
  export declare const defaultConfiguration: {
9
9
  name: null;
10
10
  port: number;
11
11
  timeout: number;
12
+ debounce: number;
13
+ maxDebounce: number;
12
14
  quiet: boolean;
13
15
  };
14
16
  /**
@@ -19,7 +21,8 @@ export declare class Hocuspocus {
19
21
  documents: Map<string, Document>;
20
22
  httpServer?: HTTPServer;
21
23
  webSocketServer?: WebSocketServer;
22
- debugger: MessageLogger;
24
+ debugger: Debugger;
25
+ constructor(configuration?: Partial<Configuration>);
23
26
  /**
24
27
  * Configure the server
25
28
  */
@@ -28,7 +31,11 @@ export declare class Hocuspocus {
28
31
  /**
29
32
  * Start the server
30
33
  */
31
- listen(portOrCallback?: number | ((data: onListenPayload) => Promise<any>) | null, callback?: any): Promise<void>;
34
+ listen(portOrCallback?: number | ((data: onListenPayload) => Promise<any>) | null, callback?: any): Promise<Hocuspocus>;
35
+ get address(): AddressInfo;
36
+ get URL(): string;
37
+ get webSocketURL(): string;
38
+ get httpURL(): string;
32
39
  private showStartScreen;
33
40
  /**
34
41
  * Get the total number of active documents
@@ -59,32 +66,35 @@ export declare class Hocuspocus {
59
66
  handleConnection(incoming: WebSocket, request: IncomingMessage, documentName: string, context?: any): void;
60
67
  /**
61
68
  * Handle update of the given document
62
- * @private
63
69
  */
64
70
  private handleDocumentUpdate;
71
+ timers: Map<string, {
72
+ timeout: NodeJS.Timeout;
73
+ start: number;
74
+ }>;
75
+ /**
76
+ * debounce the given function, using the given identifier
77
+ */
78
+ debounce(id: string, func: Function, immediately?: boolean): void;
65
79
  /**
66
80
  * Create a new document by the given request
67
- * @private
68
81
  */
69
82
  private createDocument;
70
83
  /**
71
84
  * Create a new connection by the given request and document
72
- * @private
73
85
  */
74
86
  private createConnection;
75
87
  /**
76
- * Run the given hook on all configured extensions
77
- * Runs the given callback after each hook
88
+ * Run the given hook on all configured extensions.
89
+ * Runs the given callback after each hook.
78
90
  */
79
91
  hooks(name: Hook, payload: any, callback?: Function | null): Promise<any>;
80
92
  /**
81
93
  * Get parameters by the given request
82
- * @private
83
94
  */
84
95
  private static getParameters;
85
96
  /**
86
97
  * Get document name by the given request
87
- * @private
88
98
  */
89
99
  private getDocumentNameFromRequest;
90
100
  enableDebugging(): void;
@@ -1,10 +1,13 @@
1
+ import { Awareness } from 'y-protocols/awareness';
1
2
  import Connection from './Connection';
2
3
  import { IncomingMessage } from './IncomingMessage';
3
- import { MessageLogger } from './Debugger';
4
+ import { Debugger } from './Debugger';
5
+ import Document from './Document';
4
6
  export declare class MessageReceiver {
5
7
  message: IncomingMessage;
6
- debugger: MessageLogger;
7
- constructor(message: IncomingMessage);
8
- apply(connection: Connection): void;
9
- readSyncMessage(message: IncomingMessage, connection: Connection): 0 | 1 | 2;
8
+ logger: Debugger;
9
+ constructor(message: IncomingMessage, logger: Debugger);
10
+ apply(document: Document, connection?: Connection, reply?: (message: Uint8Array) => void): void;
11
+ readSyncMessage(message: IncomingMessage, document: Document, connection?: Connection, reply?: (message: Uint8Array) => void): 0 | 1 | 2;
12
+ applyQueryAwarenessMessage(awareness: Awareness, reply?: (message: Uint8Array) => void): void;
10
13
  }
@@ -8,6 +8,7 @@ export declare class OutgoingMessage {
8
8
  constructor();
9
9
  createSyncMessage(): OutgoingMessage;
10
10
  createAwarenessUpdateMessage(awareness: Awareness, changedClients?: Array<any>): OutgoingMessage;
11
+ writeQueryAwareness(): OutgoingMessage;
11
12
  writeAuthenticated(): OutgoingMessage;
12
13
  writePermissionDenied(reason: string): OutgoingMessage;
13
14
  writeFirstSyncStepFor(document: Document): OutgoingMessage;
@@ -1,4 +1,9 @@
1
1
  export * from './Hocuspocus';
2
+ export * from './Connection';
3
+ export * from './Document';
4
+ export * from './IncomingMessage';
5
+ export * from './OutgoingMessage';
2
6
  export * from './types';
7
+ export * from './MessageReceiver';
3
8
  export * from './Document';
4
9
  export * from './Connection';
@@ -2,13 +2,15 @@
2
2
  import { IncomingHttpHeaders, IncomingMessage, ServerResponse } from 'http';
3
3
  import { URLSearchParams } from 'url';
4
4
  import { Socket } from 'net';
5
+ import { Awareness } from 'y-protocols/awareness';
5
6
  import Document from './Document';
6
7
  import { Hocuspocus } from './Hocuspocus';
7
8
  export declare enum MessageType {
8
9
  Unknown = -1,
9
10
  Sync = 0,
10
11
  Awareness = 1,
11
- Auth = 2
12
+ Auth = 2,
13
+ QueryAwareness = 3
12
14
  }
13
15
  /**
14
16
  * State of the WebSocket connection.
@@ -31,26 +33,31 @@ export interface ConnectionConfiguration {
31
33
  isAuthenticated: boolean;
32
34
  }
33
35
  export interface Extension {
34
- onAuthenticate?(data: onAuthenticatePayload): Promise<any>;
35
- onChange?(data: onChangePayload): Promise<any>;
36
- onConnect?(data: onConnectPayload): Promise<any>;
36
+ priority?: number;
37
37
  onConfigure?(data: onConfigurePayload): Promise<any>;
38
+ onListen?(data: onListenPayload): Promise<any>;
39
+ onUpgrade?(data: onUpgradePayload): Promise<any>;
40
+ onConnect?(data: onConnectPayload): Promise<any>;
41
+ onAuthenticate?(data: onAuthenticatePayload): Promise<any>;
38
42
  /**
39
43
  * @deprecated onCreateDocument is deprecated, use onLoadDocument instead
40
44
  */
41
45
  onCreateDocument?(data: onLoadDocumentPayload): Promise<any>;
42
46
  onLoadDocument?(data: onLoadDocumentPayload): Promise<any>;
43
- onDestroy?(data: onDestroyPayload): Promise<any>;
44
- onDisconnect?(data: onDisconnectPayload): Promise<any>;
45
- onListen?(data: onListenPayload): Promise<any>;
47
+ afterLoadDocument?(data: onLoadDocumentPayload): Promise<any>;
48
+ onChange?(data: onChangePayload): Promise<any>;
49
+ onStoreDocument?(data: onStoreDocumentPayload): Promise<any>;
50
+ afterStoreDocument?(data: afterStoreDocumentPayload): Promise<any>;
51
+ onAwarenessUpdate?(data: onAwarenessUpdatePayload): Promise<any>;
46
52
  onRequest?(data: onRequestPayload): Promise<any>;
47
- onUpgrade?(data: onUpgradePayload): Promise<any>;
53
+ onDisconnect?(data: onDisconnectPayload): Promise<any>;
54
+ onDestroy?(data: onDestroyPayload): Promise<any>;
48
55
  }
49
- export declare type Hook = 'onAuthenticate' | 'onChange' | 'onConnect' | 'onConfigure' |
56
+ export declare type Hook = 'onConfigure' | 'onListen' | 'onUpgrade' | 'onConnect' | 'onAuthenticate' |
50
57
  /**
51
58
  * @deprecated onCreateDocument is deprecated, use onLoadDocument instead
52
59
  */
53
- 'onCreateDocument' | 'onLoadDocument' | 'onDestroy' | 'onDisconnect' | 'onListen' | 'onRequest' | 'onUpgrade';
60
+ 'onCreateDocument' | 'onLoadDocument' | 'afterLoadDocument' | 'onChange' | 'onStoreDocument' | 'afterStoreDocument' | 'onAwarenessUpdate' | 'onRequest' | 'onDisconnect' | 'onDestroy';
54
61
  export interface Configuration extends Extension {
55
62
  /**
56
63
  * A name for the instance, used for logging.
@@ -68,6 +75,15 @@ export interface Configuration extends Extension {
68
75
  * Defines in which interval the server sends a ping, and closes the connection when no pong is sent back.
69
76
  */
70
77
  timeout: number;
78
+ /**
79
+ * Debounces the call of the `onStoreDocument` hook for the given amount of time in ms.
80
+ * Otherwise every single update would be persisted.
81
+ */
82
+ debounce: number;
83
+ /**
84
+ * Makes sure to call `onStoreDocument` at least in the given amount of time (ms).
85
+ */
86
+ maxDebounce: number;
71
87
  /**
72
88
  * By default, the servers show a start screen. If passed false, the server will start quietly.
73
89
  */
@@ -75,11 +91,12 @@ export interface Configuration extends Extension {
75
91
  /**
76
92
  * Function which returns the (customized) document name based on the request
77
93
  */
78
- getDocumentName?(data: {
79
- documentName: string;
80
- request: IncomingMessage;
81
- requestParameters: URLSearchParams;
82
- }): string | Promise<string>;
94
+ getDocumentName?(data: getDocumentNamePayload): string | Promise<string>;
95
+ }
96
+ export interface getDocumentNamePayload {
97
+ documentName: string;
98
+ request: IncomingMessage;
99
+ requestParameters: URLSearchParams;
83
100
  }
84
101
  export interface onAuthenticatePayload {
85
102
  documentName: string;
@@ -109,6 +126,16 @@ export interface onLoadDocumentPayload {
109
126
  socketId: string;
110
127
  connection: ConnectionConfiguration;
111
128
  }
129
+ export interface afterLoadDocumentPayload {
130
+ context: any;
131
+ document: Document;
132
+ documentName: string;
133
+ instance: Hocuspocus;
134
+ requestHeaders: IncomingHttpHeaders;
135
+ requestParameters: URLSearchParams;
136
+ socketId: string;
137
+ connection: ConnectionConfiguration;
138
+ }
112
139
  export interface onChangePayload {
113
140
  clientsCount: number;
114
141
  context: any;
@@ -120,6 +147,41 @@ export interface onChangePayload {
120
147
  update: Uint8Array;
121
148
  socketId: string;
122
149
  }
150
+ export interface onStoreDocumentPayload {
151
+ clientsCount: number;
152
+ context: any;
153
+ document: Document;
154
+ documentName: string;
155
+ instance: Hocuspocus;
156
+ requestHeaders: IncomingHttpHeaders;
157
+ requestParameters: URLSearchParams;
158
+ socketId: string;
159
+ }
160
+ export interface afterStoreDocumentPayload extends onStoreDocumentPayload {
161
+ }
162
+ export interface onAwarenessUpdatePayload {
163
+ clientsCount: number;
164
+ context: any;
165
+ document: Document;
166
+ documentName: string;
167
+ instance: Hocuspocus;
168
+ requestHeaders: IncomingHttpHeaders;
169
+ requestParameters: URLSearchParams;
170
+ update: Uint8Array;
171
+ socketId: string;
172
+ added: number[];
173
+ updated: number[];
174
+ removed: number[];
175
+ awareness: Awareness;
176
+ states: StatesArray;
177
+ }
178
+ export declare type StatesArray = {
179
+ clientId: number;
180
+ [key: string | number]: any;
181
+ }[];
182
+ export interface storePayload extends onStoreDocumentPayload {
183
+ state: Buffer;
184
+ }
123
185
  export interface onDisconnectPayload {
124
186
  clientsCount: number;
125
187
  context: any;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export declare const createDirectory: (dir: string) => void;
@@ -0,0 +1 @@
1
+ export declare const flushRedis: () => Promise<string>;
@@ -0,0 +1,7 @@
1
+ export * from './createDirectory';
2
+ export * from './flushRedis';
3
+ export * from './newHocuspocus';
4
+ export * from './newHocuspocusProvider';
5
+ export * from './redisConnectionSettings';
6
+ export * from './removeDirectory';
7
+ export * from './sleep';
@@ -0,0 +1,2 @@
1
+ import { Hocuspocus, Configuration } from '@hocuspocus/server';
2
+ export declare const newHocuspocus: (options?: Partial<Configuration> | undefined) => Hocuspocus;
@@ -0,0 +1,3 @@
1
+ import { HocuspocusProvider, HocuspocusProviderConfiguration } from '@hocuspocus/provider';
2
+ import { Hocuspocus } from '@hocuspocus/server';
3
+ export declare const newHocuspocusProvider: (server: Hocuspocus, options?: Partial<Omit<HocuspocusProviderConfiguration, 'url'>>) => HocuspocusProvider;
@@ -0,0 +1,4 @@
1
+ export declare const redisConnectionSettings: {
2
+ host: string;
3
+ port: number;
4
+ };
@@ -0,0 +1 @@
1
+ export declare const removeDirectory: (dir: string) => void;
@@ -0,0 +1 @@
1
+ export declare const sleep: (time: number) => Promise<unknown>;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@hocuspocus/extension-sqlite",
3
3
  "description": "a generic Hocuspocus persistence driver for the sqlite",
4
- "version": "1.0.0-alpha.5",
4
+ "version": "1.0.0-alpha.6",
5
5
  "homepage": "https://hocuspocus.dev",
6
6
  "keywords": [
7
7
  "hocuspocus",
@@ -26,7 +26,7 @@
26
26
  "dist"
27
27
  ],
28
28
  "dependencies": {
29
- "@hocuspocus/extension-database": "^1.0.0-alpha.5",
29
+ "@hocuspocus/extension-database": "^1.0.0-alpha.6",
30
30
  "kleur": "^4.1.4",
31
31
  "sqlite3": "^5.0.2"
32
32
  },
@@ -34,7 +34,7 @@
34
34
  "access": "public"
35
35
  },
36
36
  "devDependencies": {
37
- "@types/sqlite3": "^3.1.7"
37
+ "@types/sqlite3": "^3.1.8"
38
38
  },
39
- "gitHead": "90e3f0955922515ccf1d643b7b43c4ad20387735"
39
+ "gitHead": "125d94dc3520bb5185293c9b316f66ddd9215992"
40
40
  }
package/src/SQLite.ts CHANGED
@@ -2,6 +2,21 @@ import { Database, DatabaseConfiguration } from '@hocuspocus/extension-database'
2
2
  import sqlite3 from 'sqlite3'
3
3
  import kleur from 'kleur'
4
4
 
5
+ export const schema = `CREATE TABLE IF NOT EXISTS "documents" (
6
+ "name" varchar(255) NOT NULL,
7
+ "data" blob NOT NULL,
8
+ UNIQUE(name)
9
+ )`
10
+
11
+ export const selectQuery = `
12
+ SELECT data FROM "documents" WHERE name = $name ORDER BY rowid DESC
13
+ `
14
+
15
+ export const upsertQuery = `
16
+ INSERT INTO "documents" ("name", "data") VALUES ($name, $data)
17
+ ON CONFLICT(name) DO UPDATE SET data = $data
18
+ `
19
+
5
20
  export interface SQLiteConfiguration extends DatabaseConfiguration {
6
21
  /**
7
22
  * Valid values are filenames, ":memory:" for an anonymous in-memory database and an empty
@@ -22,34 +37,28 @@ export class SQLite extends Database {
22
37
 
23
38
  configuration: SQLiteConfiguration = {
24
39
  database: ':memory:',
25
- schema: `CREATE TABLE IF NOT EXISTS "documents" (
26
- "name" varchar(255) NOT NULL,
27
- "data" blob NOT NULL
28
- );`,
29
- fetchUpdates: async ({ documentName }) => {
40
+ schema,
41
+ fetch: async ({ documentName }) => {
30
42
  return new Promise((resolve, reject) => {
31
- this.db?.all('SELECT data FROM "documents" WHERE name = $name ORDER BY rowid', {
43
+ this.db?.get(selectQuery, {
32
44
  $name: documentName,
33
- }, (error, rows) => {
45
+ }, (error, row) => {
34
46
  if (error) {
35
47
  reject(error)
36
48
  }
37
49
 
38
- resolve(rows.map(row => row.data))
50
+ resolve(row?.data)
39
51
  })
40
52
  })
41
53
  },
42
- storeUpdate: async ({ documentName, update }) => {
43
- this.db?.run('INSERT INTO "documents" ("name", "data") VALUES ($name, $data)', {
54
+ store: async ({ documentName, state }) => {
55
+ this.db?.run(upsertQuery, {
44
56
  $name: documentName,
45
- $data: update,
57
+ $data: state,
46
58
  })
47
59
  },
48
60
  }
49
61
 
50
- /**
51
- * Constructor
52
- */
53
62
  constructor(configuration?: Partial<SQLiteConfiguration>) {
54
63
  super({})
55
64
 
@@ -59,14 +68,13 @@ export class SQLite extends Database {
59
68
  }
60
69
  }
61
70
 
62
- async onListen() {
63
- console.warn(` ${kleur.yellow('The SQLite extension is intended to be used in a local development environment, not in a production environment.')}`)
64
- console.log()
65
- }
66
-
67
71
  async onConfigure() {
68
72
  this.db = new sqlite3.Database(this.configuration.database)
69
-
70
73
  this.db.run(this.configuration.schema)
71
74
  }
75
+
76
+ async onListen() {
77
+ console.warn(` ${kleur.yellow('The SQLite extension is intended to be used in a local development environment, not in a production environment.')}`)
78
+ console.log()
79
+ }
72
80
  }