@quaesitor-textus/mongo 0.2.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.
Files changed (51) hide show
  1. package/LICENSE +180 -0
  2. package/README.md +417 -0
  3. package/dist/adapters/express.cjs +110 -0
  4. package/dist/adapters/express.d.cts +20 -0
  5. package/dist/adapters/express.d.ts +20 -0
  6. package/dist/adapters/express.js +7 -0
  7. package/dist/adapters/fastify.cjs +113 -0
  8. package/dist/adapters/fastify.d.cts +10 -0
  9. package/dist/adapters/fastify.d.ts +10 -0
  10. package/dist/adapters/fastify.js +16 -0
  11. package/dist/adapters/next-app.cjs +120 -0
  12. package/dist/adapters/next-app.d.cts +9 -0
  13. package/dist/adapters/next-app.d.ts +9 -0
  14. package/dist/adapters/next-app.js +23 -0
  15. package/dist/adapters/next-pages.cjs +110 -0
  16. package/dist/adapters/next-pages.d.cts +5 -0
  17. package/dist/adapters/next-pages.d.ts +5 -0
  18. package/dist/adapters/next-pages.js +7 -0
  19. package/dist/chunk-AUIK33V2.js +55 -0
  20. package/dist/chunk-RXTFVXXU.js +42 -0
  21. package/dist/index.cjs +288 -0
  22. package/dist/index.d.cts +51 -0
  23. package/dist/index.d.ts +51 -0
  24. package/dist/index.js +203 -0
  25. package/dist/startSearchSync-Bk7Na8Do.d.cts +39 -0
  26. package/dist/startSearchSync-Bk7Na8Do.d.ts +39 -0
  27. package/package.json +88 -0
  28. package/src/adapters/express.ts +8 -0
  29. package/src/adapters/fastify.ts +19 -0
  30. package/src/adapters/next-app.ts +27 -0
  31. package/src/adapters/next-pages.ts +11 -0
  32. package/src/adapters/shared.ts +61 -0
  33. package/src/buildTextSearchFilter.test.ts +30 -0
  34. package/src/buildTextSearchFilter.ts +34 -0
  35. package/src/computeSearchFields.test.ts +23 -0
  36. package/src/computeSearchFields.ts +31 -0
  37. package/src/config.ts +14 -0
  38. package/src/createLiveSearch.test.ts +48 -0
  39. package/src/createLiveSearch.ts +57 -0
  40. package/src/index.ts +12 -0
  41. package/src/modes.test.ts +20 -0
  42. package/src/modes.ts +24 -0
  43. package/src/parity.test.ts +60 -0
  44. package/src/searchIndexes.test.ts +12 -0
  45. package/src/searchIndexes.ts +22 -0
  46. package/src/sse.test.ts +11 -0
  47. package/src/sse.ts +7 -0
  48. package/src/startSearchSync.test.ts +42 -0
  49. package/src/startSearchSync.ts +91 -0
  50. package/src/version.test.ts +40 -0
  51. package/src/version.ts +41 -0
@@ -0,0 +1,110 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/adapters/express.ts
21
+ var express_exports = {};
22
+ __export(express_exports, {
23
+ streamLiveSearch: () => streamToNodeResponse
24
+ });
25
+ module.exports = __toCommonJS(express_exports);
26
+
27
+ // src/createLiveSearch.ts
28
+ function createLiveSearch(opts) {
29
+ const { sync, collection, config: _config, filter, sort, cap = 500, sendEvent } = opts;
30
+ const seen = /* @__PURE__ */ new Set();
31
+ let count = 0;
32
+ let capped = false;
33
+ const idOf = (doc) => String(doc._id);
34
+ const cursor = collection.find(filter);
35
+ if (sort) cursor.sort({ [sort.field]: sort.dir });
36
+ void cursor.limit(cap).toArray().then((items) => {
37
+ for (const it of items) seen.add(idOf(it));
38
+ count = items.length;
39
+ sendEvent({ type: "snapshot", items });
40
+ if (count >= cap) {
41
+ capped = true;
42
+ sendEvent({ type: "capped" });
43
+ }
44
+ }).catch(() => sendEvent({ type: "snapshot", items: [] }));
45
+ const listener = (e) => {
46
+ if (e.type !== "indexed" || capped) return;
47
+ void collection.findOne({ $and: [{ _id: e.id }, filter] }).then((doc) => {
48
+ if (!doc || capped) return;
49
+ const id = idOf(doc);
50
+ if (seen.has(id)) return;
51
+ seen.add(id);
52
+ count += 1;
53
+ sendEvent({ type: "match", item: doc });
54
+ if (count >= cap) {
55
+ capped = true;
56
+ sendEvent({ type: "capped" });
57
+ }
58
+ }).catch(() => {
59
+ });
60
+ };
61
+ sync.on(listener);
62
+ return { stop: () => sync.off(listener) };
63
+ }
64
+
65
+ // src/sse.ts
66
+ function formatSse(event) {
67
+ return `data: ${JSON.stringify(event)}
68
+
69
+ `;
70
+ }
71
+ function sseComment(text = "ping") {
72
+ return `: ${text}
73
+
74
+ `;
75
+ }
76
+
77
+ // src/adapters/shared.ts
78
+ var SSE_HEADERS = {
79
+ "Content-Type": "text/event-stream",
80
+ "Cache-Control": "no-cache",
81
+ Connection: "keep-alive"
82
+ };
83
+ function runLiveSearch(opts, write) {
84
+ const sendEvent = (e) => write(formatSse(e));
85
+ const live = createLiveSearch({
86
+ sync: opts.sync,
87
+ collection: opts.collection,
88
+ config: opts.config,
89
+ filter: opts.filter,
90
+ sort: opts.sort,
91
+ cap: opts.cap,
92
+ sendEvent
93
+ });
94
+ const hb = setInterval(() => write(sseComment()), opts.heartbeatMs ?? 25e3);
95
+ return {
96
+ stop: () => {
97
+ clearInterval(hb);
98
+ live.stop();
99
+ }
100
+ };
101
+ }
102
+ function streamToNodeResponse(req, res, opts) {
103
+ res.writeHead(200, SSE_HEADERS);
104
+ const { stop } = runLiveSearch(opts, (chunk) => res.write(chunk));
105
+ req.on("close", stop);
106
+ }
107
+ // Annotate the CommonJS export names for ESM import in node:
108
+ 0 && (module.exports = {
109
+ streamLiveSearch
110
+ });
@@ -0,0 +1,20 @@
1
+ import { Collection, Filter, Document } from 'mongodb';
2
+ import { IncomingMessage, ServerResponse } from 'http';
3
+ import { S as SearchSync, a as MongoSearchConfig } from '../startSearchSync-Bk7Na8Do.cjs';
4
+ import '@quaesitor-textus/core';
5
+
6
+ interface StreamLiveSearchOptions {
7
+ sync: SearchSync;
8
+ collection: Collection;
9
+ config: MongoSearchConfig;
10
+ filter: Filter<Document>;
11
+ sort?: {
12
+ field: string;
13
+ dir: 1 | -1;
14
+ };
15
+ cap?: number;
16
+ heartbeatMs?: number;
17
+ }
18
+ declare function streamToNodeResponse(req: IncomingMessage, res: ServerResponse, opts: StreamLiveSearchOptions): void;
19
+
20
+ export { type StreamLiveSearchOptions, streamToNodeResponse as streamLiveSearch };
@@ -0,0 +1,20 @@
1
+ import { Collection, Filter, Document } from 'mongodb';
2
+ import { IncomingMessage, ServerResponse } from 'http';
3
+ import { S as SearchSync, a as MongoSearchConfig } from '../startSearchSync-Bk7Na8Do.js';
4
+ import '@quaesitor-textus/core';
5
+
6
+ interface StreamLiveSearchOptions {
7
+ sync: SearchSync;
8
+ collection: Collection;
9
+ config: MongoSearchConfig;
10
+ filter: Filter<Document>;
11
+ sort?: {
12
+ field: string;
13
+ dir: 1 | -1;
14
+ };
15
+ cap?: number;
16
+ heartbeatMs?: number;
17
+ }
18
+ declare function streamToNodeResponse(req: IncomingMessage, res: ServerResponse, opts: StreamLiveSearchOptions): void;
19
+
20
+ export { type StreamLiveSearchOptions, streamToNodeResponse as streamLiveSearch };
@@ -0,0 +1,7 @@
1
+ import {
2
+ streamToNodeResponse
3
+ } from "../chunk-RXTFVXXU.js";
4
+ import "../chunk-AUIK33V2.js";
5
+ export {
6
+ streamToNodeResponse as streamLiveSearch
7
+ };
@@ -0,0 +1,113 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/adapters/fastify.ts
21
+ var fastify_exports = {};
22
+ __export(fastify_exports, {
23
+ streamLiveSearch: () => streamLiveSearch
24
+ });
25
+ module.exports = __toCommonJS(fastify_exports);
26
+
27
+ // src/createLiveSearch.ts
28
+ function createLiveSearch(opts) {
29
+ const { sync, collection, config: _config, filter, sort, cap = 500, sendEvent } = opts;
30
+ const seen = /* @__PURE__ */ new Set();
31
+ let count = 0;
32
+ let capped = false;
33
+ const idOf = (doc) => String(doc._id);
34
+ const cursor = collection.find(filter);
35
+ if (sort) cursor.sort({ [sort.field]: sort.dir });
36
+ void cursor.limit(cap).toArray().then((items) => {
37
+ for (const it of items) seen.add(idOf(it));
38
+ count = items.length;
39
+ sendEvent({ type: "snapshot", items });
40
+ if (count >= cap) {
41
+ capped = true;
42
+ sendEvent({ type: "capped" });
43
+ }
44
+ }).catch(() => sendEvent({ type: "snapshot", items: [] }));
45
+ const listener = (e) => {
46
+ if (e.type !== "indexed" || capped) return;
47
+ void collection.findOne({ $and: [{ _id: e.id }, filter] }).then((doc) => {
48
+ if (!doc || capped) return;
49
+ const id = idOf(doc);
50
+ if (seen.has(id)) return;
51
+ seen.add(id);
52
+ count += 1;
53
+ sendEvent({ type: "match", item: doc });
54
+ if (count >= cap) {
55
+ capped = true;
56
+ sendEvent({ type: "capped" });
57
+ }
58
+ }).catch(() => {
59
+ });
60
+ };
61
+ sync.on(listener);
62
+ return { stop: () => sync.off(listener) };
63
+ }
64
+
65
+ // src/sse.ts
66
+ function formatSse(event) {
67
+ return `data: ${JSON.stringify(event)}
68
+
69
+ `;
70
+ }
71
+ function sseComment(text = "ping") {
72
+ return `: ${text}
73
+
74
+ `;
75
+ }
76
+
77
+ // src/adapters/shared.ts
78
+ var SSE_HEADERS = {
79
+ "Content-Type": "text/event-stream",
80
+ "Cache-Control": "no-cache",
81
+ Connection: "keep-alive"
82
+ };
83
+ function runLiveSearch(opts, write) {
84
+ const sendEvent = (e) => write(formatSse(e));
85
+ const live = createLiveSearch({
86
+ sync: opts.sync,
87
+ collection: opts.collection,
88
+ config: opts.config,
89
+ filter: opts.filter,
90
+ sort: opts.sort,
91
+ cap: opts.cap,
92
+ sendEvent
93
+ });
94
+ const hb = setInterval(() => write(sseComment()), opts.heartbeatMs ?? 25e3);
95
+ return {
96
+ stop: () => {
97
+ clearInterval(hb);
98
+ live.stop();
99
+ }
100
+ };
101
+ }
102
+
103
+ // src/adapters/fastify.ts
104
+ function streamLiveSearch(request, reply, opts) {
105
+ reply.raw.writeHead(200, SSE_HEADERS);
106
+ reply.hijack();
107
+ const { stop } = runLiveSearch(opts, (chunk) => reply.raw.write(chunk));
108
+ request.raw.on("close", stop);
109
+ }
110
+ // Annotate the CommonJS export names for ESM import in node:
111
+ 0 && (module.exports = {
112
+ streamLiveSearch
113
+ });
@@ -0,0 +1,10 @@
1
+ import { FastifyRequest, FastifyReply } from 'fastify';
2
+ import { StreamLiveSearchOptions } from './express.cjs';
3
+ import 'mongodb';
4
+ import 'http';
5
+ import '../startSearchSync-Bk7Na8Do.cjs';
6
+ import '@quaesitor-textus/core';
7
+
8
+ declare function streamLiveSearch(request: FastifyRequest, reply: FastifyReply, opts: StreamLiveSearchOptions): void;
9
+
10
+ export { StreamLiveSearchOptions, streamLiveSearch };
@@ -0,0 +1,10 @@
1
+ import { FastifyRequest, FastifyReply } from 'fastify';
2
+ import { StreamLiveSearchOptions } from './express.js';
3
+ import 'mongodb';
4
+ import 'http';
5
+ import '../startSearchSync-Bk7Na8Do.js';
6
+ import '@quaesitor-textus/core';
7
+
8
+ declare function streamLiveSearch(request: FastifyRequest, reply: FastifyReply, opts: StreamLiveSearchOptions): void;
9
+
10
+ export { StreamLiveSearchOptions, streamLiveSearch };
@@ -0,0 +1,16 @@
1
+ import {
2
+ SSE_HEADERS,
3
+ runLiveSearch
4
+ } from "../chunk-RXTFVXXU.js";
5
+ import "../chunk-AUIK33V2.js";
6
+
7
+ // src/adapters/fastify.ts
8
+ function streamLiveSearch(request, reply, opts) {
9
+ reply.raw.writeHead(200, SSE_HEADERS);
10
+ reply.hijack();
11
+ const { stop } = runLiveSearch(opts, (chunk) => reply.raw.write(chunk));
12
+ request.raw.on("close", stop);
13
+ }
14
+ export {
15
+ streamLiveSearch
16
+ };
@@ -0,0 +1,120 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/adapters/next-app.ts
21
+ var next_app_exports = {};
22
+ __export(next_app_exports, {
23
+ liveSearchResponse: () => liveSearchResponse
24
+ });
25
+ module.exports = __toCommonJS(next_app_exports);
26
+
27
+ // src/createLiveSearch.ts
28
+ function createLiveSearch(opts) {
29
+ const { sync, collection, config: _config, filter, sort, cap = 500, sendEvent } = opts;
30
+ const seen = /* @__PURE__ */ new Set();
31
+ let count = 0;
32
+ let capped = false;
33
+ const idOf = (doc) => String(doc._id);
34
+ const cursor = collection.find(filter);
35
+ if (sort) cursor.sort({ [sort.field]: sort.dir });
36
+ void cursor.limit(cap).toArray().then((items) => {
37
+ for (const it of items) seen.add(idOf(it));
38
+ count = items.length;
39
+ sendEvent({ type: "snapshot", items });
40
+ if (count >= cap) {
41
+ capped = true;
42
+ sendEvent({ type: "capped" });
43
+ }
44
+ }).catch(() => sendEvent({ type: "snapshot", items: [] }));
45
+ const listener = (e) => {
46
+ if (e.type !== "indexed" || capped) return;
47
+ void collection.findOne({ $and: [{ _id: e.id }, filter] }).then((doc) => {
48
+ if (!doc || capped) return;
49
+ const id = idOf(doc);
50
+ if (seen.has(id)) return;
51
+ seen.add(id);
52
+ count += 1;
53
+ sendEvent({ type: "match", item: doc });
54
+ if (count >= cap) {
55
+ capped = true;
56
+ sendEvent({ type: "capped" });
57
+ }
58
+ }).catch(() => {
59
+ });
60
+ };
61
+ sync.on(listener);
62
+ return { stop: () => sync.off(listener) };
63
+ }
64
+
65
+ // src/sse.ts
66
+ function formatSse(event) {
67
+ return `data: ${JSON.stringify(event)}
68
+
69
+ `;
70
+ }
71
+ function sseComment(text = "ping") {
72
+ return `: ${text}
73
+
74
+ `;
75
+ }
76
+
77
+ // src/adapters/shared.ts
78
+ var SSE_HEADERS = {
79
+ "Content-Type": "text/event-stream",
80
+ "Cache-Control": "no-cache",
81
+ Connection: "keep-alive"
82
+ };
83
+ function runLiveSearch(opts, write) {
84
+ const sendEvent = (e) => write(formatSse(e));
85
+ const live = createLiveSearch({
86
+ sync: opts.sync,
87
+ collection: opts.collection,
88
+ config: opts.config,
89
+ filter: opts.filter,
90
+ sort: opts.sort,
91
+ cap: opts.cap,
92
+ sendEvent
93
+ });
94
+ const hb = setInterval(() => write(sseComment()), opts.heartbeatMs ?? 25e3);
95
+ return {
96
+ stop: () => {
97
+ clearInterval(hb);
98
+ live.stop();
99
+ }
100
+ };
101
+ }
102
+
103
+ // src/adapters/next-app.ts
104
+ function liveSearchResponse(opts) {
105
+ const encoder = new TextEncoder();
106
+ let handle;
107
+ const stream = new ReadableStream({
108
+ start(controller) {
109
+ handle = runLiveSearch(opts, (chunk) => controller.enqueue(encoder.encode(chunk)));
110
+ },
111
+ cancel() {
112
+ handle?.stop();
113
+ }
114
+ });
115
+ return new Response(stream, { headers: SSE_HEADERS });
116
+ }
117
+ // Annotate the CommonJS export names for ESM import in node:
118
+ 0 && (module.exports = {
119
+ liveSearchResponse
120
+ });
@@ -0,0 +1,9 @@
1
+ import { StreamLiveSearchOptions } from './express.cjs';
2
+ import 'mongodb';
3
+ import 'http';
4
+ import '../startSearchSync-Bk7Na8Do.cjs';
5
+ import '@quaesitor-textus/core';
6
+
7
+ declare function liveSearchResponse(opts: StreamLiveSearchOptions): Response;
8
+
9
+ export { StreamLiveSearchOptions, liveSearchResponse };
@@ -0,0 +1,9 @@
1
+ import { StreamLiveSearchOptions } from './express.js';
2
+ import 'mongodb';
3
+ import 'http';
4
+ import '../startSearchSync-Bk7Na8Do.js';
5
+ import '@quaesitor-textus/core';
6
+
7
+ declare function liveSearchResponse(opts: StreamLiveSearchOptions): Response;
8
+
9
+ export { StreamLiveSearchOptions, liveSearchResponse };
@@ -0,0 +1,23 @@
1
+ import {
2
+ SSE_HEADERS,
3
+ runLiveSearch
4
+ } from "../chunk-RXTFVXXU.js";
5
+ import "../chunk-AUIK33V2.js";
6
+
7
+ // src/adapters/next-app.ts
8
+ function liveSearchResponse(opts) {
9
+ const encoder = new TextEncoder();
10
+ let handle;
11
+ const stream = new ReadableStream({
12
+ start(controller) {
13
+ handle = runLiveSearch(opts, (chunk) => controller.enqueue(encoder.encode(chunk)));
14
+ },
15
+ cancel() {
16
+ handle?.stop();
17
+ }
18
+ });
19
+ return new Response(stream, { headers: SSE_HEADERS });
20
+ }
21
+ export {
22
+ liveSearchResponse
23
+ };
@@ -0,0 +1,110 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/adapters/next-pages.ts
21
+ var next_pages_exports = {};
22
+ __export(next_pages_exports, {
23
+ streamLiveSearch: () => streamToNodeResponse
24
+ });
25
+ module.exports = __toCommonJS(next_pages_exports);
26
+
27
+ // src/createLiveSearch.ts
28
+ function createLiveSearch(opts) {
29
+ const { sync, collection, config: _config, filter, sort, cap = 500, sendEvent } = opts;
30
+ const seen = /* @__PURE__ */ new Set();
31
+ let count = 0;
32
+ let capped = false;
33
+ const idOf = (doc) => String(doc._id);
34
+ const cursor = collection.find(filter);
35
+ if (sort) cursor.sort({ [sort.field]: sort.dir });
36
+ void cursor.limit(cap).toArray().then((items) => {
37
+ for (const it of items) seen.add(idOf(it));
38
+ count = items.length;
39
+ sendEvent({ type: "snapshot", items });
40
+ if (count >= cap) {
41
+ capped = true;
42
+ sendEvent({ type: "capped" });
43
+ }
44
+ }).catch(() => sendEvent({ type: "snapshot", items: [] }));
45
+ const listener = (e) => {
46
+ if (e.type !== "indexed" || capped) return;
47
+ void collection.findOne({ $and: [{ _id: e.id }, filter] }).then((doc) => {
48
+ if (!doc || capped) return;
49
+ const id = idOf(doc);
50
+ if (seen.has(id)) return;
51
+ seen.add(id);
52
+ count += 1;
53
+ sendEvent({ type: "match", item: doc });
54
+ if (count >= cap) {
55
+ capped = true;
56
+ sendEvent({ type: "capped" });
57
+ }
58
+ }).catch(() => {
59
+ });
60
+ };
61
+ sync.on(listener);
62
+ return { stop: () => sync.off(listener) };
63
+ }
64
+
65
+ // src/sse.ts
66
+ function formatSse(event) {
67
+ return `data: ${JSON.stringify(event)}
68
+
69
+ `;
70
+ }
71
+ function sseComment(text = "ping") {
72
+ return `: ${text}
73
+
74
+ `;
75
+ }
76
+
77
+ // src/adapters/shared.ts
78
+ var SSE_HEADERS = {
79
+ "Content-Type": "text/event-stream",
80
+ "Cache-Control": "no-cache",
81
+ Connection: "keep-alive"
82
+ };
83
+ function runLiveSearch(opts, write) {
84
+ const sendEvent = (e) => write(formatSse(e));
85
+ const live = createLiveSearch({
86
+ sync: opts.sync,
87
+ collection: opts.collection,
88
+ config: opts.config,
89
+ filter: opts.filter,
90
+ sort: opts.sort,
91
+ cap: opts.cap,
92
+ sendEvent
93
+ });
94
+ const hb = setInterval(() => write(sseComment()), opts.heartbeatMs ?? 25e3);
95
+ return {
96
+ stop: () => {
97
+ clearInterval(hb);
98
+ live.stop();
99
+ }
100
+ };
101
+ }
102
+ function streamToNodeResponse(req, res, opts) {
103
+ res.writeHead(200, SSE_HEADERS);
104
+ const { stop } = runLiveSearch(opts, (chunk) => res.write(chunk));
105
+ req.on("close", stop);
106
+ }
107
+ // Annotate the CommonJS export names for ESM import in node:
108
+ 0 && (module.exports = {
109
+ streamLiveSearch
110
+ });
@@ -0,0 +1,5 @@
1
+ export { StreamLiveSearchOptions, streamLiveSearch } from './express.cjs';
2
+ import 'mongodb';
3
+ import 'http';
4
+ import '../startSearchSync-Bk7Na8Do.cjs';
5
+ import '@quaesitor-textus/core';
@@ -0,0 +1,5 @@
1
+ export { StreamLiveSearchOptions, streamLiveSearch } from './express.js';
2
+ import 'mongodb';
3
+ import 'http';
4
+ import '../startSearchSync-Bk7Na8Do.js';
5
+ import '@quaesitor-textus/core';
@@ -0,0 +1,7 @@
1
+ import {
2
+ streamToNodeResponse
3
+ } from "../chunk-RXTFVXXU.js";
4
+ import "../chunk-AUIK33V2.js";
5
+ export {
6
+ streamToNodeResponse as streamLiveSearch
7
+ };
@@ -0,0 +1,55 @@
1
+ // src/createLiveSearch.ts
2
+ function createLiveSearch(opts) {
3
+ const { sync, collection, config: _config, filter, sort, cap = 500, sendEvent } = opts;
4
+ const seen = /* @__PURE__ */ new Set();
5
+ let count = 0;
6
+ let capped = false;
7
+ const idOf = (doc) => String(doc._id);
8
+ const cursor = collection.find(filter);
9
+ if (sort) cursor.sort({ [sort.field]: sort.dir });
10
+ void cursor.limit(cap).toArray().then((items) => {
11
+ for (const it of items) seen.add(idOf(it));
12
+ count = items.length;
13
+ sendEvent({ type: "snapshot", items });
14
+ if (count >= cap) {
15
+ capped = true;
16
+ sendEvent({ type: "capped" });
17
+ }
18
+ }).catch(() => sendEvent({ type: "snapshot", items: [] }));
19
+ const listener = (e) => {
20
+ if (e.type !== "indexed" || capped) return;
21
+ void collection.findOne({ $and: [{ _id: e.id }, filter] }).then((doc) => {
22
+ if (!doc || capped) return;
23
+ const id = idOf(doc);
24
+ if (seen.has(id)) return;
25
+ seen.add(id);
26
+ count += 1;
27
+ sendEvent({ type: "match", item: doc });
28
+ if (count >= cap) {
29
+ capped = true;
30
+ sendEvent({ type: "capped" });
31
+ }
32
+ }).catch(() => {
33
+ });
34
+ };
35
+ sync.on(listener);
36
+ return { stop: () => sync.off(listener) };
37
+ }
38
+
39
+ // src/sse.ts
40
+ function formatSse(event) {
41
+ return `data: ${JSON.stringify(event)}
42
+
43
+ `;
44
+ }
45
+ function sseComment(text = "ping") {
46
+ return `: ${text}
47
+
48
+ `;
49
+ }
50
+
51
+ export {
52
+ createLiveSearch,
53
+ formatSse,
54
+ sseComment
55
+ };