@naturalcycles/backend-lib 9.46.0 → 9.46.2

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.
@@ -20,8 +20,8 @@ export interface AdminInfo {
20
20
  * Base implementation based on Firebase Auth tokens passed as 'admin_token' cookie.
21
21
  */
22
22
  export declare class BaseAdminService {
23
- private firebaseAuth;
24
- constructor(firebaseAuth: FirebaseAdmin.auth.Auth, cfg: AdminServiceCfg);
23
+ private loadFirebaseAuth;
24
+ constructor(loadFirebaseAuth: () => Promise<FirebaseAdmin.auth.Auth>, cfg: AdminServiceCfg);
25
25
  cfg: Required<AdminServiceCfg>;
26
26
  adminInfoDisabled(): AdminInfo;
27
27
  /**
@@ -37,6 +37,7 @@ export declare class BaseAdminService {
37
37
  */
38
38
  protected onPermissionCheck(req: BackendRequest, email: string, reqPermissions: string[], required: boolean, granted: boolean, meta?: Record<string, any>): Promise<void>;
39
39
  getEmailByToken(req: BackendRequest, adminToken?: string): Promise<string | undefined>;
40
+ private getFirebaseAuth;
40
41
  /**
41
42
  * Current implementation is based on req=Request (from Express).
42
43
  * Override if needed.
@@ -1,3 +1,5 @@
1
+ import { __decorate } from "tslib";
2
+ import { _Memo } from '@naturalcycles/js-lib/decorators';
1
3
  import { _assert, AppError } from '@naturalcycles/js-lib/error';
2
4
  import { dimGrey, green, red } from '@naturalcycles/nodejs-lib/colors';
3
5
  const adminInfoDisabled = () => ({
@@ -8,9 +10,9 @@ const adminInfoDisabled = () => ({
8
10
  * Base implementation based on Firebase Auth tokens passed as 'admin_token' cookie.
9
11
  */
10
12
  export class BaseAdminService {
11
- firebaseAuth;
12
- constructor(firebaseAuth, cfg) {
13
- this.firebaseAuth = firebaseAuth;
13
+ loadFirebaseAuth;
14
+ constructor(loadFirebaseAuth, cfg) {
15
+ this.loadFirebaseAuth = loadFirebaseAuth;
14
16
  this.cfg = {
15
17
  adminTokenKey: 'admin_token',
16
18
  authEnabled: true,
@@ -47,7 +49,8 @@ export class BaseAdminService {
47
49
  if (!adminToken)
48
50
  return;
49
51
  try {
50
- const decodedToken = await this.firebaseAuth.verifyIdToken(adminToken);
52
+ const auth = await this.getFirebaseAuth();
53
+ const decodedToken = await auth.verifyIdToken(adminToken);
51
54
  const email = decodedToken?.email;
52
55
  req.log(`admin email: ${dimGrey(email)}`);
53
56
  return email;
@@ -63,6 +66,9 @@ export class BaseAdminService {
63
66
  req.error(`getEmailByToken error:`, err);
64
67
  }
65
68
  }
69
+ async getFirebaseAuth() {
70
+ return await this.loadFirebaseAuth();
71
+ }
66
72
  /**
67
73
  * Current implementation is based on req=Request (from Express).
68
74
  * Override if needed.
@@ -193,3 +199,6 @@ export class BaseAdminService {
193
199
  };
194
200
  }
195
201
  }
202
+ __decorate([
203
+ _Memo()
204
+ ], BaseAdminService.prototype, "getFirebaseAuth", null);
@@ -157,8 +157,11 @@ export function compressionMiddleware(options) {
157
157
  return;
158
158
  }
159
159
  // compression method
160
+ // Get all client-accepted encodings, then pick the first one from our preferred order
160
161
  const negotiator = new Negotiator(req);
161
- let method = negotiator.encoding(SUPPORTED_ENCODING, PREFERRED_ENCODING);
162
+ const clientEncodings = negotiator.encodings(SUPPORTED_ENCODING);
163
+ // Prefer server's order, but fall back to client's first choice if no preferred match
164
+ let method = PREFERRED_ENCODING.find(enc => clientEncodings.includes(enc)) || clientEncodings[0];
162
165
  // if no method is found, use the default encoding
163
166
  if (!req.headers['accept-encoding'] && encodingSupported.has(enforceEncoding)) {
164
167
  method = enforceEncoding;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@naturalcycles/backend-lib",
3
3
  "type": "module",
4
- "version": "9.46.0",
4
+ "version": "9.46.2",
5
5
  "peerDependencies": {
6
6
  "@sentry/node": "^10"
7
7
  },
@@ -1,3 +1,4 @@
1
+ import { _Memo } from '@naturalcycles/js-lib/decorators'
1
2
  import { _assert, AppError } from '@naturalcycles/js-lib/error'
2
3
  import { dimGrey, green, red } from '@naturalcycles/nodejs-lib/colors'
3
4
  import type FirebaseAdmin from 'firebase-admin'
@@ -32,7 +33,7 @@ const adminInfoDisabled = (): AdminInfo => ({
32
33
  */
33
34
  export class BaseAdminService {
34
35
  constructor(
35
- private firebaseAuth: FirebaseAdmin.auth.Auth,
36
+ private loadFirebaseAuth: () => Promise<FirebaseAdmin.auth.Auth>,
36
37
  cfg: AdminServiceCfg,
37
38
  ) {
38
39
  this.cfg = {
@@ -91,7 +92,8 @@ export class BaseAdminService {
91
92
  if (!adminToken) return
92
93
 
93
94
  try {
94
- const decodedToken = await this.firebaseAuth.verifyIdToken(adminToken)
95
+ const auth = await this.getFirebaseAuth()
96
+ const decodedToken = await auth.verifyIdToken(adminToken)
95
97
  const email = decodedToken?.email
96
98
  req.log(`admin email: ${dimGrey(email)}`)
97
99
  return email
@@ -109,6 +111,11 @@ export class BaseAdminService {
109
111
  }
110
112
  }
111
113
 
114
+ @_Memo()
115
+ private async getFirebaseAuth(): Promise<FirebaseAdmin.auth.Auth> {
116
+ return await this.loadFirebaseAuth()
117
+ }
118
+
112
119
  /**
113
120
  * Current implementation is based on req=Request (from Express).
114
121
  * Override if needed.
@@ -239,8 +239,12 @@ export function compressionMiddleware(options?: CompressionOptions): BackendRequ
239
239
  }
240
240
 
241
241
  // compression method
242
+ // Get all client-accepted encodings, then pick the first one from our preferred order
242
243
  const negotiator = new Negotiator(req)
243
- let method: string | undefined = negotiator.encoding(SUPPORTED_ENCODING, PREFERRED_ENCODING)
244
+ const clientEncodings = negotiator.encodings(SUPPORTED_ENCODING) as string[]
245
+ // Prefer server's order, but fall back to client's first choice if no preferred match
246
+ let method: string | undefined =
247
+ PREFERRED_ENCODING.find(enc => clientEncodings.includes(enc)) || clientEncodings[0]
244
248
 
245
249
  // if no method is found, use the default encoding
246
250
  if (!req.headers['accept-encoding'] && encodingSupported.has(enforceEncoding)) {