@akanjs/server 0.0.39 → 0.0.40
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/dbDecorators-z3rJpvxp.d.ts +36 -0
- package/index.d.ts +23 -0
- package/index.js +20 -3780
- package/package.json +2 -44
- package/src/boot.d.ts +49 -0
- package/src/boot.js +258 -0
- package/src/controller.d.ts +9 -0
- package/src/controller.js +96 -0
- package/src/gql.d.ts +77 -0
- package/src/gql.js +172 -0
- package/src/index.d.ts +23 -0
- package/src/index.js +39 -0
- package/src/module.d.ts +95 -0
- package/src/module.js +347 -0
- package/src/processor.d.ts +11 -0
- package/src/processor.js +85 -0
- package/src/resolver.d.ts +9 -0
- package/src/resolver.js +161 -0
- package/src/schema.d.ts +12 -0
- package/src/schema.js +275 -0
- package/src/searchDaemon.d.ts +13 -0
- package/src/searchDaemon.js +282 -0
- package/src/types.d.ts +18 -0
- package/src/types.js +15 -0
- package/src/websocket.d.ts +20 -0
- package/src/websocket.js +192 -0
- package/types-CUkhtrLV.d.ts +53 -0
- package/types-H2HI4YUe.d.ts +3 -0
|
@@ -0,0 +1,282 @@
|
|
|
1
|
+
var __create = Object.create;
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
var searchDaemon_exports = {};
|
|
30
|
+
__export(searchDaemon_exports, {
|
|
31
|
+
SearchDaemonModule: () => SearchDaemonModule,
|
|
32
|
+
makeTextFilter: () => makeTextFilter
|
|
33
|
+
});
|
|
34
|
+
module.exports = __toCommonJS(searchDaemon_exports);
|
|
35
|
+
var import_common = require("@akanjs/common");
|
|
36
|
+
var import_constant = require("@akanjs/constant");
|
|
37
|
+
var import_document = require("@akanjs/document");
|
|
38
|
+
var import_common2 = require("@nestjs/common");
|
|
39
|
+
var import_mongoose = require("@nestjs/mongoose");
|
|
40
|
+
var import_meilisearch = __toESM(require("meilisearch"));
|
|
41
|
+
function _ts_decorate(decorators, target, key, desc) {
|
|
42
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
43
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
44
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
45
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
46
|
+
}
|
|
47
|
+
__name(_ts_decorate, "_ts_decorate");
|
|
48
|
+
function _ts_metadata(k, v) {
|
|
49
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
50
|
+
}
|
|
51
|
+
__name(_ts_metadata, "_ts_metadata");
|
|
52
|
+
function _ts_param(paramIndex, decorator) {
|
|
53
|
+
return function(target, key) {
|
|
54
|
+
decorator(target, key, paramIndex);
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
__name(_ts_param, "_ts_param");
|
|
58
|
+
const hasTextField = /* @__PURE__ */ __name((modelRef) => {
|
|
59
|
+
const fieldMetas = (0, import_constant.getFieldMetas)(modelRef);
|
|
60
|
+
return fieldMetas.some((fieldMeta) => !!fieldMeta.text || fieldMeta.isScalar && fieldMeta.isClass && fieldMeta.select && hasTextField(fieldMeta.modelRef));
|
|
61
|
+
}, "hasTextField");
|
|
62
|
+
const getTextFieldKeys = /* @__PURE__ */ __name((modelRef) => {
|
|
63
|
+
const allSearchFields = [];
|
|
64
|
+
const allFilterFields = [];
|
|
65
|
+
const fieldMetaMap = (0, import_constant.getFieldMetaMap)(modelRef);
|
|
66
|
+
const fieldMetas = [
|
|
67
|
+
...fieldMetaMap.values()
|
|
68
|
+
];
|
|
69
|
+
const stringTextFields = fieldMetas.filter((fieldMeta) => !!fieldMeta.text).map((fieldMeta) => {
|
|
70
|
+
if (fieldMeta.text === "filter") allFilterFields.push(fieldMeta.key);
|
|
71
|
+
else if (fieldMeta.text === "search") allSearchFields.push(fieldMeta.key);
|
|
72
|
+
return fieldMeta.key;
|
|
73
|
+
});
|
|
74
|
+
const scalarTextFields = fieldMetas.filter((fieldMeta) => fieldMeta.isScalar && fieldMeta.isClass && fieldMeta.select && hasTextField(fieldMeta.modelRef)).map((fieldMeta) => fieldMeta.key);
|
|
75
|
+
const deepFields = scalarTextFields.map((key) => {
|
|
76
|
+
const fieldMeta = fieldMetaMap.get(key);
|
|
77
|
+
if (!fieldMeta) throw new Error(`No fieldMeta for ${key}`);
|
|
78
|
+
const { stringTextFields: stringTextFields2, allTextFields, allSearchFields: allSearchFields2, allFilterFields: allFilterFields2 } = getTextFieldKeys(fieldMeta.modelRef);
|
|
79
|
+
allFilterFields2.push(...allSearchFields2.map((field) => `${key}.${field}`));
|
|
80
|
+
allSearchFields2.push(...stringTextFields2.map((field) => `${key}.${field}`));
|
|
81
|
+
return [
|
|
82
|
+
...stringTextFields2.map((field) => `${key}.${field}`),
|
|
83
|
+
...allTextFields.map((field) => `${key}.${field}`)
|
|
84
|
+
];
|
|
85
|
+
}).flat();
|
|
86
|
+
return {
|
|
87
|
+
stringTextFields,
|
|
88
|
+
scalarTextFields,
|
|
89
|
+
allTextFields: [
|
|
90
|
+
...stringTextFields,
|
|
91
|
+
...deepFields
|
|
92
|
+
],
|
|
93
|
+
allSearchFields,
|
|
94
|
+
allFilterFields
|
|
95
|
+
};
|
|
96
|
+
}, "getTextFieldKeys");
|
|
97
|
+
const makeTextFilter = /* @__PURE__ */ __name((modelRef) => {
|
|
98
|
+
const fieldMetaMap = (0, import_constant.getFieldMetaMap)(modelRef);
|
|
99
|
+
const { stringTextFields, scalarTextFields } = getTextFieldKeys(modelRef);
|
|
100
|
+
const filterData = /* @__PURE__ */ __name((data, assignObj = {}) => {
|
|
101
|
+
if (Array.isArray(data)) return data.map((d) => filterData(d));
|
|
102
|
+
return Object.assign(Object.fromEntries([
|
|
103
|
+
...stringTextFields.map((key) => [
|
|
104
|
+
key,
|
|
105
|
+
data[key]
|
|
106
|
+
]),
|
|
107
|
+
...scalarTextFields.map((key) => {
|
|
108
|
+
const fieldMeta = fieldMetaMap.get(key);
|
|
109
|
+
if (!fieldMeta) throw new Error(`No fieldMeta for ${key}`);
|
|
110
|
+
const filterFunc = makeTextFilter(fieldMeta.modelRef);
|
|
111
|
+
return [
|
|
112
|
+
key,
|
|
113
|
+
filterFunc(data[key])
|
|
114
|
+
];
|
|
115
|
+
})
|
|
116
|
+
]), assignObj);
|
|
117
|
+
}, "filterData");
|
|
118
|
+
return filterData;
|
|
119
|
+
}, "makeTextFilter");
|
|
120
|
+
const getSortableAttributes = /* @__PURE__ */ __name((refName) => {
|
|
121
|
+
const cnst = (0, import_constant.getCnstMeta)(refName);
|
|
122
|
+
const sortMap = (0, import_constant.getFilterSortMap)(cnst.Filter);
|
|
123
|
+
const sortFields = Object.values(sortMap).filter((val) => typeof val === "object").map((sort) => Object.keys(sort)).flat();
|
|
124
|
+
return [
|
|
125
|
+
...new Set(sortFields)
|
|
126
|
+
];
|
|
127
|
+
}, "getSortableAttributes");
|
|
128
|
+
let SearchDaemon = class SearchDaemon2 {
|
|
129
|
+
static {
|
|
130
|
+
__name(this, "SearchDaemon");
|
|
131
|
+
}
|
|
132
|
+
connection;
|
|
133
|
+
meili;
|
|
134
|
+
logger;
|
|
135
|
+
constructor(connection, meili) {
|
|
136
|
+
this.connection = connection;
|
|
137
|
+
this.meili = meili;
|
|
138
|
+
this.logger = new import_common.Logger("SearchDaemon");
|
|
139
|
+
}
|
|
140
|
+
async onModuleInit() {
|
|
141
|
+
const databaseModelNames = (0, import_document.getAllDatabaseModelNames)();
|
|
142
|
+
const indexes = (await this.meili.getIndexes({
|
|
143
|
+
limit: 1e3
|
|
144
|
+
})).results;
|
|
145
|
+
const indexMap = new Map(indexes.map((index) => [
|
|
146
|
+
index.uid,
|
|
147
|
+
index
|
|
148
|
+
]));
|
|
149
|
+
const indexCreationNames = [];
|
|
150
|
+
const indexUpdateNames = [];
|
|
151
|
+
for (const modelName of databaseModelNames) {
|
|
152
|
+
const indexName = (0, import_common.lowerlize)(modelName);
|
|
153
|
+
const modelRef = (0, import_constant.getFullModelRef)(modelName);
|
|
154
|
+
if (!hasTextField(modelRef)) continue;
|
|
155
|
+
const index = indexMap.get(indexName);
|
|
156
|
+
if (!index) indexCreationNames.push(indexName);
|
|
157
|
+
else if (index.primaryKey !== "id") indexUpdateNames.push(indexName);
|
|
158
|
+
}
|
|
159
|
+
for (const indexName of indexCreationNames) await this.meili.createIndex(indexName, {
|
|
160
|
+
primaryKey: "id"
|
|
161
|
+
});
|
|
162
|
+
for (const indexName of indexUpdateNames) await this.meili.updateIndex(indexName, {
|
|
163
|
+
primaryKey: "id"
|
|
164
|
+
});
|
|
165
|
+
for (const modelName of databaseModelNames) {
|
|
166
|
+
const indexName = (0, import_common.lowerlize)(modelName);
|
|
167
|
+
const model = this.connection.models[modelName];
|
|
168
|
+
const modelRef = (0, import_constant.getFullModelRef)(modelName);
|
|
169
|
+
if (!hasTextField(modelRef)) continue;
|
|
170
|
+
const searchIndex = this.meili.index(indexName);
|
|
171
|
+
const { stringTextFields, scalarTextFields, allSearchFields, allFilterFields } = getTextFieldKeys(modelRef);
|
|
172
|
+
const settings = await searchIndex.getSettings();
|
|
173
|
+
const allSearchFieldSet = new Set(allSearchFields);
|
|
174
|
+
const allFilterFieldSet = new Set(allFilterFields);
|
|
175
|
+
const searchFieldSet = new Set(settings.searchableAttributes);
|
|
176
|
+
const filterFieldSet = new Set(settings.filterableAttributes);
|
|
177
|
+
const needUpdateSetting = !allSearchFields.every((field) => searchFieldSet.has(field)) || !allFilterFields.every((field) => filterFieldSet.has(field)) || !settings.searchableAttributes?.every((field) => allSearchFieldSet.has(field)) || !settings.filterableAttributes?.every((field) => allFilterFieldSet.has(field));
|
|
178
|
+
if (needUpdateSetting) {
|
|
179
|
+
this.logger.info(`update index settings (${modelName})`);
|
|
180
|
+
await searchIndex.updateSettings({
|
|
181
|
+
searchableAttributes: allSearchFields,
|
|
182
|
+
filterableAttributes: allFilterFields,
|
|
183
|
+
sortableAttributes: getSortableAttributes(indexName)
|
|
184
|
+
});
|
|
185
|
+
}
|
|
186
|
+
const stringTextFieldSet = new Set(stringTextFields);
|
|
187
|
+
const scalarTextFieldSet = new Set(scalarTextFields);
|
|
188
|
+
const filterText = makeTextFilter(modelRef);
|
|
189
|
+
model.watch().on("change", async (data) => {
|
|
190
|
+
try {
|
|
191
|
+
const id = data.documentKey._id.toString();
|
|
192
|
+
if (data.operationType === "delete") {
|
|
193
|
+
this.logger.trace(`delete text doc (${modelName}): ${id}`);
|
|
194
|
+
return await searchIndex.deleteDocument(id);
|
|
195
|
+
} else if (data.operationType === "insert") {
|
|
196
|
+
this.logger.trace(`insert text doc (${modelName}): ${data.documentKey._id}`);
|
|
197
|
+
if (!data.fullDocument) throw new Error("No fullDocument");
|
|
198
|
+
const textFilteredData = filterText(data.fullDocument);
|
|
199
|
+
return await searchIndex.addDocuments([
|
|
200
|
+
textFilteredData
|
|
201
|
+
]);
|
|
202
|
+
} else if (data.operationType === "update") {
|
|
203
|
+
const updatedFields = data.updateDescription?.updatedFields ?? {};
|
|
204
|
+
const isRemoved = !!updatedFields.removedAt;
|
|
205
|
+
if (isRemoved) {
|
|
206
|
+
this.logger.trace(`remove text doc (${modelName}): ${id}`);
|
|
207
|
+
return await searchIndex.deleteDocument(id);
|
|
208
|
+
}
|
|
209
|
+
this.logger.trace(`update text doc (${modelName}): ${data.documentKey._id}`);
|
|
210
|
+
const updatedFieldKeys = Object.keys(updatedFields);
|
|
211
|
+
const removedFieldKeys = data.updateDescription?.removedFields ?? [];
|
|
212
|
+
const isScalarTextFieldUpdated = [
|
|
213
|
+
...updatedFieldKeys,
|
|
214
|
+
...removedFieldKeys
|
|
215
|
+
].map((key) => key.split(".")[0]).some((key) => scalarTextFieldSet.has(key));
|
|
216
|
+
if (isScalarTextFieldUpdated) {
|
|
217
|
+
const doc = await model.findById(data.documentKey._id);
|
|
218
|
+
if (!doc) this.logger.error(`No doc for ${data.documentKey._id}`);
|
|
219
|
+
const textFilteredData = filterText(doc, {
|
|
220
|
+
id
|
|
221
|
+
});
|
|
222
|
+
return await searchIndex.updateDocuments([
|
|
223
|
+
textFilteredData
|
|
224
|
+
]);
|
|
225
|
+
} else {
|
|
226
|
+
const updateKeys = updatedFieldKeys.filter((key) => stringTextFieldSet.has(key));
|
|
227
|
+
const removeKeys = removedFieldKeys.filter((key) => stringTextFieldSet.has(key));
|
|
228
|
+
if (!updateKeys.length && !removeKeys.length) return;
|
|
229
|
+
const textFilteredData = Object.fromEntries([
|
|
230
|
+
[
|
|
231
|
+
"id",
|
|
232
|
+
id
|
|
233
|
+
],
|
|
234
|
+
...updateKeys.map((key) => [
|
|
235
|
+
key,
|
|
236
|
+
updatedFields[key]
|
|
237
|
+
]),
|
|
238
|
+
...removeKeys.map((key) => [
|
|
239
|
+
key,
|
|
240
|
+
null
|
|
241
|
+
])
|
|
242
|
+
]);
|
|
243
|
+
return await searchIndex.updateDocuments([
|
|
244
|
+
textFilteredData
|
|
245
|
+
]);
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
} catch (e) {
|
|
249
|
+
this.logger.error(e);
|
|
250
|
+
}
|
|
251
|
+
});
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
};
|
|
255
|
+
SearchDaemon = _ts_decorate([
|
|
256
|
+
(0, import_common2.Injectable)(),
|
|
257
|
+
_ts_param(0, (0, import_mongoose.InjectConnection)()),
|
|
258
|
+
_ts_param(1, (0, import_common2.Inject)("MEILI_CLIENT")),
|
|
259
|
+
_ts_metadata("design:type", Function),
|
|
260
|
+
_ts_metadata("design:paramtypes", [
|
|
261
|
+
typeof Connection === "undefined" ? Object : Connection,
|
|
262
|
+
typeof import_meilisearch.default === "undefined" ? Object : import_meilisearch.default
|
|
263
|
+
])
|
|
264
|
+
], SearchDaemon);
|
|
265
|
+
class SearchDaemonModule {
|
|
266
|
+
static {
|
|
267
|
+
__name(this, "SearchDaemonModule");
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
SearchDaemonModule = _ts_decorate([
|
|
271
|
+
(0, import_common2.Global)(),
|
|
272
|
+
(0, import_common2.Module)({
|
|
273
|
+
providers: [
|
|
274
|
+
SearchDaemon
|
|
275
|
+
]
|
|
276
|
+
})
|
|
277
|
+
], SearchDaemonModule);
|
|
278
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
279
|
+
0 && (module.exports = {
|
|
280
|
+
SearchDaemonModule,
|
|
281
|
+
makeTextFilter
|
|
282
|
+
});
|
package/src/types.d.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { ReadStream } from 'fs';
|
|
2
|
+
import { Readable } from 'stream';
|
|
3
|
+
export { DynamicModule as Module } from '@nestjs/common';
|
|
4
|
+
|
|
5
|
+
interface FileStream {
|
|
6
|
+
filename: string;
|
|
7
|
+
mimetype: string;
|
|
8
|
+
encoding: string;
|
|
9
|
+
createReadStream(): ReadStream | Readable;
|
|
10
|
+
}
|
|
11
|
+
interface LocalFile {
|
|
12
|
+
filename: string;
|
|
13
|
+
mimetype: string;
|
|
14
|
+
encoding: string;
|
|
15
|
+
localPath: string;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export type { FileStream, LocalFile };
|
package/src/types.js
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __copyProps = (to, from, except, desc) => {
|
|
6
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
7
|
+
for (let key of __getOwnPropNames(from))
|
|
8
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
9
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
10
|
+
}
|
|
11
|
+
return to;
|
|
12
|
+
};
|
|
13
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
14
|
+
var types_exports = {};
|
|
15
|
+
module.exports = __toCommonJS(types_exports);
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { T as Type } from '../types-H2HI4YUe.js';
|
|
2
|
+
import { Server } from 'socket.io';
|
|
3
|
+
|
|
4
|
+
interface Response<T> {
|
|
5
|
+
data: T;
|
|
6
|
+
}
|
|
7
|
+
declare const websocketOf: (sigRef: Type, allSrvs: {
|
|
8
|
+
[key: string]: Type;
|
|
9
|
+
}) => {
|
|
10
|
+
new (): {
|
|
11
|
+
__sigRef__: Type;
|
|
12
|
+
};
|
|
13
|
+
};
|
|
14
|
+
declare const websocketServerOf: (sigRef: Type) => {
|
|
15
|
+
new (): {
|
|
16
|
+
server: Server;
|
|
17
|
+
};
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
export { type Response, websocketOf, websocketServerOf };
|
package/src/websocket.js
ADDED
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
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
|
+
var websocket_exports = {};
|
|
20
|
+
__export(websocket_exports, {
|
|
21
|
+
websocketOf: () => websocketOf,
|
|
22
|
+
websocketServerOf: () => websocketServerOf
|
|
23
|
+
});
|
|
24
|
+
module.exports = __toCommonJS(websocket_exports);
|
|
25
|
+
var import_common = require("@akanjs/common");
|
|
26
|
+
var import_nest = require("@akanjs/nest");
|
|
27
|
+
var import_service = require("@akanjs/service");
|
|
28
|
+
var import_signal = require("@akanjs/signal");
|
|
29
|
+
var import_common2 = require("@nestjs/common");
|
|
30
|
+
var import_websockets = require("@nestjs/websockets");
|
|
31
|
+
var import_operators = require("rxjs/operators");
|
|
32
|
+
function _ts_decorate(decorators, target, key, desc) {
|
|
33
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
34
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
35
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
36
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
37
|
+
}
|
|
38
|
+
__name(_ts_decorate, "_ts_decorate");
|
|
39
|
+
function _ts_metadata(k, v) {
|
|
40
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
41
|
+
}
|
|
42
|
+
__name(_ts_metadata, "_ts_metadata");
|
|
43
|
+
const internalArgMap = {
|
|
44
|
+
Account: import_nest.Account,
|
|
45
|
+
UserIp: import_nest.UserIp,
|
|
46
|
+
Access: import_nest.Access,
|
|
47
|
+
Self: import_nest.Self,
|
|
48
|
+
Me: import_nest.Me,
|
|
49
|
+
Ws: import_nest.Ws
|
|
50
|
+
};
|
|
51
|
+
let TransformInterceptor = class TransformInterceptor2 {
|
|
52
|
+
static {
|
|
53
|
+
__name(this, "TransformInterceptor");
|
|
54
|
+
}
|
|
55
|
+
intercept(context, next) {
|
|
56
|
+
const [, gqlKey] = [
|
|
57
|
+
context.getArgByIndex(1),
|
|
58
|
+
context.getArgByIndex(3)
|
|
59
|
+
];
|
|
60
|
+
return next.handle().pipe((0, import_operators.map)((data) => ({
|
|
61
|
+
event: gqlKey,
|
|
62
|
+
data
|
|
63
|
+
})));
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
TransformInterceptor = _ts_decorate([
|
|
67
|
+
(0, import_common2.Injectable)()
|
|
68
|
+
], TransformInterceptor);
|
|
69
|
+
const makeRoomId = /* @__PURE__ */ __name((gqlKey, argValues) => `${gqlKey}-${argValues.join("-")}`, "makeRoomId");
|
|
70
|
+
const getPubsubInterceptor = /* @__PURE__ */ __name((argMetas) => {
|
|
71
|
+
let PubsubInterceptor = class PubsubInterceptor {
|
|
72
|
+
static {
|
|
73
|
+
__name(this, "PubsubInterceptor");
|
|
74
|
+
}
|
|
75
|
+
async intercept(context, next) {
|
|
76
|
+
const [socket, { __subscribe__, ...body }, gqlKey] = [
|
|
77
|
+
context.getArgByIndex(0),
|
|
78
|
+
context.getArgByIndex(1),
|
|
79
|
+
context.getArgByIndex(3)
|
|
80
|
+
];
|
|
81
|
+
const roomId = makeRoomId(gqlKey, argMetas.map((argMeta) => body[argMeta.name]));
|
|
82
|
+
if (__subscribe__) await socket.join(roomId);
|
|
83
|
+
else await socket.leave(roomId);
|
|
84
|
+
return next.handle().pipe((0, import_operators.map)(() => ({
|
|
85
|
+
event: gqlKey,
|
|
86
|
+
data: {
|
|
87
|
+
roomId,
|
|
88
|
+
__subscribe__
|
|
89
|
+
}
|
|
90
|
+
})));
|
|
91
|
+
}
|
|
92
|
+
};
|
|
93
|
+
PubsubInterceptor = _ts_decorate([
|
|
94
|
+
(0, import_common2.Injectable)()
|
|
95
|
+
], PubsubInterceptor);
|
|
96
|
+
return PubsubInterceptor;
|
|
97
|
+
}, "getPubsubInterceptor");
|
|
98
|
+
const websocketOf = /* @__PURE__ */ __name((sigRef, allSrvs) => {
|
|
99
|
+
const sigMeta = (0, import_signal.getSigMeta)(sigRef);
|
|
100
|
+
let WsGateway = class WsGateway {
|
|
101
|
+
static {
|
|
102
|
+
__name(this, "WsGateway");
|
|
103
|
+
}
|
|
104
|
+
__sigRef__ = sigRef;
|
|
105
|
+
};
|
|
106
|
+
Object.keys(allSrvs).forEach((srv) => {
|
|
107
|
+
if (!(0, import_service.isServiceEnabled)(allSrvs[srv])) return;
|
|
108
|
+
(0, import_common2.Inject)(allSrvs[srv])(WsGateway.prototype, (0, import_common.lowerlize)(srv));
|
|
109
|
+
});
|
|
110
|
+
const messageGqlMetas = (0, import_signal.getGqlMetas)(sigRef).filter((gqlMeta) => gqlMeta.type === "Message");
|
|
111
|
+
for (const gqlMeta of messageGqlMetas) {
|
|
112
|
+
const descriptor = {
|
|
113
|
+
...Object.getOwnPropertyDescriptor(sigRef.prototype, gqlMeta.key) ?? {}
|
|
114
|
+
};
|
|
115
|
+
Object.defineProperty(WsGateway.prototype, gqlMeta.key, descriptor);
|
|
116
|
+
const [argMetas, internalArgMetas] = (0, import_signal.getArgMetas)(sigRef, gqlMeta.key);
|
|
117
|
+
argMetas.forEach((argMeta) => {
|
|
118
|
+
if (argMeta.type !== "Msg") throw new Error(`Argument of Message should be Msg ${sigMeta.refName}-${gqlMeta.key}-${argMeta.name}`);
|
|
119
|
+
(0, import_websockets.MessageBody)(argMeta.name, ...(0, import_nest.getBodyPipes)(argMeta))(WsGateway.prototype, gqlMeta.key, argMeta.idx);
|
|
120
|
+
});
|
|
121
|
+
internalArgMetas.forEach((internalArgMeta) => {
|
|
122
|
+
const internalDecorator = internalArgMap[internalArgMeta.type];
|
|
123
|
+
internalDecorator(internalArgMeta.option ?? {})(WsGateway.prototype, gqlMeta.key, internalArgMeta.idx);
|
|
124
|
+
});
|
|
125
|
+
(0, import_common2.UseInterceptors)(TransformInterceptor)(WsGateway.prototype, gqlMeta.key, gqlMeta.descriptor);
|
|
126
|
+
(0, import_websockets.SubscribeMessage)(gqlMeta.key)(WsGateway.prototype, gqlMeta.key, descriptor);
|
|
127
|
+
}
|
|
128
|
+
const pubsubGqlMetas = (0, import_signal.getGqlMetas)(sigRef).filter((gqlMeta) => gqlMeta.type === "Pubsub");
|
|
129
|
+
for (const gqlMeta of pubsubGqlMetas) {
|
|
130
|
+
const descriptor = {
|
|
131
|
+
...Object.getOwnPropertyDescriptor(sigRef.prototype, gqlMeta.key) ?? {}
|
|
132
|
+
};
|
|
133
|
+
Object.defineProperty(WsGateway.prototype, gqlMeta.key, descriptor);
|
|
134
|
+
const [argMetas, internalArgMetas] = (0, import_signal.getArgMetas)(sigRef, gqlMeta.key);
|
|
135
|
+
argMetas.forEach((argMeta) => {
|
|
136
|
+
if (argMeta.type !== "Room") throw new Error(`Argument of Message should be Room ${sigMeta.refName}-${gqlMeta.key}-${argMeta.name}`);
|
|
137
|
+
(0, import_websockets.MessageBody)(argMeta.name, ...(0, import_nest.getBodyPipes)(argMeta))(WsGateway.prototype, gqlMeta.key, argMeta.idx);
|
|
138
|
+
});
|
|
139
|
+
internalArgMetas.forEach((internalArgMeta) => {
|
|
140
|
+
const internalDecorator = internalArgMap[internalArgMeta.type];
|
|
141
|
+
internalDecorator(internalArgMeta.option ?? {})(WsGateway.prototype, gqlMeta.key, internalArgMeta.idx);
|
|
142
|
+
});
|
|
143
|
+
(0, import_common2.UseInterceptors)(getPubsubInterceptor(argMetas))(WsGateway.prototype, gqlMeta.key, gqlMeta.descriptor);
|
|
144
|
+
(0, import_websockets.SubscribeMessage)(gqlMeta.key)(WsGateway.prototype, gqlMeta.key, descriptor);
|
|
145
|
+
}
|
|
146
|
+
(0, import_websockets.WebSocketGateway)({
|
|
147
|
+
cors: {
|
|
148
|
+
origin: "*"
|
|
149
|
+
},
|
|
150
|
+
transports: [
|
|
151
|
+
"websocket"
|
|
152
|
+
]
|
|
153
|
+
})(WsGateway);
|
|
154
|
+
return WsGateway;
|
|
155
|
+
}, "websocketOf");
|
|
156
|
+
const websocketServerOf = /* @__PURE__ */ __name((sigRef) => {
|
|
157
|
+
const pubsubGqlMetas = (0, import_signal.getGqlMetas)(sigRef).filter((gqlMeta) => gqlMeta.type === "Pubsub");
|
|
158
|
+
let Websocket = class Websocket {
|
|
159
|
+
static {
|
|
160
|
+
__name(this, "Websocket");
|
|
161
|
+
}
|
|
162
|
+
server;
|
|
163
|
+
};
|
|
164
|
+
_ts_decorate([
|
|
165
|
+
(0, import_websockets.WebSocketServer)(),
|
|
166
|
+
_ts_metadata("design:type", typeof Server === "undefined" ? Object : Server)
|
|
167
|
+
], Websocket.prototype, "server", void 0);
|
|
168
|
+
Websocket = _ts_decorate([
|
|
169
|
+
(0, import_common2.Injectable)(),
|
|
170
|
+
(0, import_websockets.WebSocketGateway)({
|
|
171
|
+
cors: {
|
|
172
|
+
origin: "*"
|
|
173
|
+
},
|
|
174
|
+
transports: [
|
|
175
|
+
"websocket"
|
|
176
|
+
]
|
|
177
|
+
})
|
|
178
|
+
], Websocket);
|
|
179
|
+
for (const gqlMeta of pubsubGqlMetas) {
|
|
180
|
+
const [argMetas] = (0, import_signal.getArgMetas)(sigRef, gqlMeta.key);
|
|
181
|
+
Websocket.prototype[gqlMeta.key] = function(...args) {
|
|
182
|
+
const roomId = makeRoomId(gqlMeta.key, argMetas.map((argMeta) => args[argMeta.idx]));
|
|
183
|
+
this.server.to(roomId).emit(roomId, args.at(-1));
|
|
184
|
+
};
|
|
185
|
+
}
|
|
186
|
+
return Websocket;
|
|
187
|
+
}, "websocketServerOf");
|
|
188
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
189
|
+
0 && (module.exports = {
|
|
190
|
+
websocketOf,
|
|
191
|
+
websocketServerOf
|
|
192
|
+
});
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { Dayjs } from 'dayjs';
|
|
2
|
+
import { ReadStream } from 'fs';
|
|
3
|
+
import { Readable } from 'stream';
|
|
4
|
+
|
|
5
|
+
declare class BaseObject {
|
|
6
|
+
id: string;
|
|
7
|
+
createdAt: Dayjs;
|
|
8
|
+
updatedAt: Dayjs;
|
|
9
|
+
removedAt: Dayjs | null;
|
|
10
|
+
}
|
|
11
|
+
declare class Int {
|
|
12
|
+
__Scalar__: "int";
|
|
13
|
+
}
|
|
14
|
+
declare class Upload {
|
|
15
|
+
__Scalar__: "upload";
|
|
16
|
+
filename: string;
|
|
17
|
+
mimetype: string;
|
|
18
|
+
encoding: string;
|
|
19
|
+
createReadStream: () => ReadStream | Readable;
|
|
20
|
+
}
|
|
21
|
+
declare class Float {
|
|
22
|
+
__Scalar__: "float";
|
|
23
|
+
}
|
|
24
|
+
declare class ID {
|
|
25
|
+
__Scalar__: "id";
|
|
26
|
+
}
|
|
27
|
+
declare class JSON {
|
|
28
|
+
__Scalar__: "json";
|
|
29
|
+
}
|
|
30
|
+
declare const gqlScalars: readonly [StringConstructor, BooleanConstructor, DateConstructor, typeof ID, typeof Int, typeof Float, typeof Upload, typeof JSON, MapConstructor];
|
|
31
|
+
type GqlScalar = (typeof gqlScalars)[number];
|
|
32
|
+
|
|
33
|
+
type Optional<T, K extends keyof T> = Pick<Partial<T>, K> & Omit<T, K>;
|
|
34
|
+
type OptionalKeys<T> = T extends {
|
|
35
|
+
[key: string]: any;
|
|
36
|
+
} ? {
|
|
37
|
+
[K in keyof T]-?: null extends T[K] ? K : never;
|
|
38
|
+
}[keyof T] : never;
|
|
39
|
+
type ObjectToId<O, D = Dayjs> = O extends BaseObject ? string : O extends BaseObject[] ? string[] : O extends Dayjs ? D : O extends {
|
|
40
|
+
[key: string]: any;
|
|
41
|
+
} ? DocumentModel<O> : O;
|
|
42
|
+
type FilterType = Record<string, any>;
|
|
43
|
+
type DocumentModel<T, D = Dayjs> = T extends (infer S)[] ? DocumentModel<S>[] : T extends string ? T : T extends number ? T : T extends boolean ? T : T extends Dayjs ? T : T extends Map<infer K, infer V> ? Map<K, DocumentModel<V, D>> : Optional<{
|
|
44
|
+
[K in keyof GetStateObject<T>]: T[K] extends infer S ? S extends null ? undefined : ObjectToId<T[K], D> : never;
|
|
45
|
+
}, OptionalKeys<GetStateObject<T>>>;
|
|
46
|
+
type GetStateObject<T> = Omit<{
|
|
47
|
+
[K in keyof T as T[K] extends (...args: any) => any ? never : K]: T[K];
|
|
48
|
+
}, "prototype">;
|
|
49
|
+
interface TextDoc {
|
|
50
|
+
[key: string]: string | TextDoc;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export { BaseObject as B, type DocumentModel as D, type FilterType as F, type GqlScalar as G, type TextDoc as T };
|