@radatek/microserver 2.3.8 → 2.3.10
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/microserver.d.ts +9 -3
- package/microserver.js +31 -16
- package/package.json +1 -1
package/microserver.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* MicroServer
|
|
3
|
-
* @version 2.3.
|
|
3
|
+
* @version 2.3.10
|
|
4
4
|
* @package @radatek/microserver
|
|
5
5
|
* @copyright Darius Kisonas 2022
|
|
6
6
|
* @license MIT
|
|
@@ -120,7 +120,7 @@ export declare class ServerResponse<T = any> extends http.ServerResponse {
|
|
|
120
120
|
redirect(code: number | string, url?: string): void;
|
|
121
121
|
/** Set status code */
|
|
122
122
|
status(code: number): this;
|
|
123
|
-
|
|
123
|
+
file(path: string, filename?: string): void;
|
|
124
124
|
}
|
|
125
125
|
/** WebSocket options */
|
|
126
126
|
export interface WebSocketOptions {
|
|
@@ -551,6 +551,8 @@ export interface AuthOptionsInternal extends AuthOptions {
|
|
|
551
551
|
};
|
|
552
552
|
/** Expire time in seconds */
|
|
553
553
|
expire: number;
|
|
554
|
+
/** Use object token instead of user id */
|
|
555
|
+
objectToken: boolean;
|
|
554
556
|
/** Authentication mode */
|
|
555
557
|
mode: 'cookie' | 'token';
|
|
556
558
|
/** Authentication realm for basic authentication */
|
|
@@ -782,8 +784,12 @@ export declare class Model<TSchema extends ModelSchema> {
|
|
|
782
784
|
insert(data: Record<string, any>, options?: ModelContextOptions): Promise<ModelDocument<TSchema>>;
|
|
783
785
|
/** Update one matching document */
|
|
784
786
|
update(query: Record<string, any>, options?: ModelContextOptions): Promise<ModelDocument<TSchema>>;
|
|
787
|
+
/** Update many matching documents */
|
|
788
|
+
updateMany(query: Record<string, any>, update: Record<string, any>, options?: ModelContextOptions): Promise<ModelDocument<TSchema>>;
|
|
785
789
|
/** Delete one matching document */
|
|
786
|
-
delete(query: Query, options?: ModelContextOptions): Promise<
|
|
790
|
+
delete(query: Query, options?: ModelContextOptions): Promise<number>;
|
|
791
|
+
/** Delete many matching documents */
|
|
792
|
+
deleteMany(query: Query, options?: ModelContextOptions): Promise<number>;
|
|
787
793
|
/** Microserver middleware */
|
|
788
794
|
handler(req: ServerRequest, res: ServerResponse): any;
|
|
789
795
|
}
|
package/microserver.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* MicroServer
|
|
3
|
-
* @version 2.3.
|
|
3
|
+
* @version 2.3.10
|
|
4
4
|
* @package @radatek/microserver
|
|
5
5
|
* @copyright Darius Kisonas 2022
|
|
6
6
|
* @license MIT
|
|
@@ -438,7 +438,7 @@ export class ServerResponse extends http.ServerResponse {
|
|
|
438
438
|
this.statusCode = code;
|
|
439
439
|
return this;
|
|
440
440
|
}
|
|
441
|
-
|
|
441
|
+
file(path, filename) {
|
|
442
442
|
StaticPlugin.serveFile(this.req, this, {
|
|
443
443
|
path: path,
|
|
444
444
|
filename: filename || basename(path),
|
|
@@ -987,7 +987,7 @@ export class Router extends EventEmitter {
|
|
|
987
987
|
idx = 5;
|
|
988
988
|
}
|
|
989
989
|
if (name === 'json')
|
|
990
|
-
return (req, res) => res.isJson = true;
|
|
990
|
+
return (req, res, next) => { res.isJson = true; return next(); };
|
|
991
991
|
if (idx >= 0) {
|
|
992
992
|
const v = name.slice(idx + 1);
|
|
993
993
|
const type = name.slice(0, idx);
|
|
@@ -2109,7 +2109,7 @@ export class Auth {
|
|
|
2109
2109
|
if (usrInfo?.id || usrInfo?._id) {
|
|
2110
2110
|
const expire = Math.min(34560000, options?.expire || this.options.expire || defaultExpire);
|
|
2111
2111
|
const expireTime = new Date().getTime() + expire * 1000;
|
|
2112
|
-
const token = await this.token((usrInfo?.id || usrInfo?._id), undefined, expire);
|
|
2112
|
+
const token = await this.token(this.options.objectToken ? JSON.stringify(usrInfo) : (usrInfo?.id || usrInfo?._id), undefined, expire);
|
|
2113
2113
|
if (token && this.res && this.req) {
|
|
2114
2114
|
const oldToken = this.req.tokenId;
|
|
2115
2115
|
if (oldToken)
|
|
@@ -2313,14 +2313,14 @@ class AuthPlugin extends Plugin {
|
|
|
2313
2313
|
}
|
|
2314
2314
|
return next();
|
|
2315
2315
|
}
|
|
2316
|
-
const cookie = req.headers.cookie, cookies = cookie ? cookie.split(/;\s
|
|
2316
|
+
const cookie = req.headers.cookie, cookies = cookie ? cookie.split(/;\s*/g) : [];
|
|
2317
2317
|
const sid = cookies.find(s => s.startsWith('token='));
|
|
2318
2318
|
let token = '';
|
|
2319
2319
|
if (authorization.startsWith('Bearer '))
|
|
2320
2320
|
token = authorization.slice(7);
|
|
2321
2321
|
if (sid)
|
|
2322
2322
|
token = sid.slice(sid.indexOf('=') + 1);
|
|
2323
|
-
if (
|
|
2323
|
+
if (req.query.token)
|
|
2324
2324
|
token = req.query.token;
|
|
2325
2325
|
if (token) {
|
|
2326
2326
|
const now = new Date().getTime();
|
|
@@ -2913,8 +2913,6 @@ export class Model {
|
|
|
2913
2913
|
/** Find one document */
|
|
2914
2914
|
async findOne(query, options) {
|
|
2915
2915
|
const collection = await this.collection;
|
|
2916
|
-
if (!collection)
|
|
2917
|
-
throw new AccessDenied('Database not configured');
|
|
2918
2916
|
options = { readOnly: true, ...this.options, ...options };
|
|
2919
2917
|
const doc = await collection.findOne(this.getFilter(query, options));
|
|
2920
2918
|
return doc ? this.document(doc, options) : undefined;
|
|
@@ -2922,8 +2920,6 @@ export class Model {
|
|
|
2922
2920
|
/** Find many documents */
|
|
2923
2921
|
async findMany(query, options) {
|
|
2924
2922
|
const collection = await this.collection;
|
|
2925
|
-
if (!collection)
|
|
2926
|
-
throw new AccessDenied('Database not configured');
|
|
2927
2923
|
const res = [];
|
|
2928
2924
|
options = { readOnly: true, ...this.options, ...options };
|
|
2929
2925
|
await collection.find(this.getFilter(query || {}, options)).forEach((doc) => res.push(this.document(doc, options)));
|
|
@@ -2936,8 +2932,6 @@ export class Model {
|
|
|
2936
2932
|
/** Update one matching document */
|
|
2937
2933
|
async update(query, options) {
|
|
2938
2934
|
const collection = await this.collection;
|
|
2939
|
-
if (!collection)
|
|
2940
|
-
throw new AccessDenied('Database not configured');
|
|
2941
2935
|
options = { ...this.options, ...options };
|
|
2942
2936
|
if (options?.validate !== false)
|
|
2943
2937
|
query = this.document(query, options);
|
|
@@ -2954,13 +2948,34 @@ export class Model {
|
|
|
2954
2948
|
throw new NotFound('Document not found');
|
|
2955
2949
|
return res;
|
|
2956
2950
|
}
|
|
2951
|
+
/** Update many matching documents */
|
|
2952
|
+
async updateMany(query, update, options) {
|
|
2953
|
+
const collection = await this.collection;
|
|
2954
|
+
options = { ...this.options, ...options };
|
|
2955
|
+
if (options?.validate !== false)
|
|
2956
|
+
update = this.document(update, options);
|
|
2957
|
+
const unset = query.$unset || {};
|
|
2958
|
+
for (const n in update) {
|
|
2959
|
+
if (update[n] === undefined || update[n] === null) {
|
|
2960
|
+
update.$unset = unset;
|
|
2961
|
+
unset[n] = 1;
|
|
2962
|
+
delete update[n];
|
|
2963
|
+
}
|
|
2964
|
+
}
|
|
2965
|
+
const res = await collection.updateMany(this.getFilter(query, { primaryKey: true, validate: false }), update);
|
|
2966
|
+
if (!res)
|
|
2967
|
+
throw new NotFound('Document not found');
|
|
2968
|
+
return res;
|
|
2969
|
+
}
|
|
2957
2970
|
/** Delete one matching document */
|
|
2958
2971
|
async delete(query, options) {
|
|
2959
2972
|
const collection = await this.collection;
|
|
2960
|
-
|
|
2961
|
-
|
|
2962
|
-
|
|
2963
|
-
|
|
2973
|
+
return await collection.deleteOne(this.getFilter(query, { ...this.options, ...options }));
|
|
2974
|
+
}
|
|
2975
|
+
/** Delete many matching documents */
|
|
2976
|
+
async deleteMany(query, options) {
|
|
2977
|
+
const collection = await this.collection;
|
|
2978
|
+
return await collection.deleteMany(this.getFilter(query, { ...this.options, ...options }));
|
|
2964
2979
|
}
|
|
2965
2980
|
/** Microserver middleware */
|
|
2966
2981
|
handler(req, res) {
|