Skip to content

Commit da7c11f

Browse files
committed
lint: lib/csp.ts
add typing annotate Signed-off-by: Raccoon <raccoon@hackmd.io>
1 parent 7680d64 commit da7c11f

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

lib/csp.ts

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
1+
import {RequestHandler} from "express";
12
import config from "./config";
23
import uuid from "uuid";
34

4-
const CspStrategy: any = {}
5+
6+
const CspStrategy: {
7+
computeDirectives?: () => Record<string, string[]>
8+
addNonceToLocals?: RequestHandler
9+
} = {}
510

611
const defaultDirectives = {
712
defaultSrc: ['\'self\''],
@@ -37,7 +42,7 @@ const googleAnalyticsDirectives = {
3742
}
3843

3944
CspStrategy.computeDirectives = function () {
40-
const directives = {}
45+
const directives: Record<string, string[]> = {}
4146
mergeDirectives(directives, config.csp.directives)
4247
mergeDirectivesIf(config.csp.addDefaults, directives, defaultDirectives)
4348
mergeDirectivesIf(config.useCDN, directives, cdnDirectives)
@@ -52,12 +57,14 @@ CspStrategy.computeDirectives = function () {
5257
return directives
5358
}
5459

55-
function mergeDirectives(existingDirectives, newDirectives) {
60+
function mergeDirectives(existingDirectives: Record<string, string[]>, newDirectives: Record<string, string[]>) {
5661
for (const propertyName in newDirectives) {
57-
const newDirective = newDirectives[propertyName]
58-
if (newDirective) {
59-
const existingDirective = existingDirectives[propertyName] || []
60-
existingDirectives[propertyName] = existingDirective.concat(newDirective)
62+
if (Object.hasOwnProperty.call(newDirectives, propertyName)) {
63+
const newDirective = newDirectives[propertyName]
64+
if (newDirective) {
65+
const existingDirective = existingDirectives[propertyName] || []
66+
existingDirectives[propertyName] = existingDirective.concat(newDirective)
67+
}
6168
}
6269
}
6370
}

0 commit comments

Comments
 (0)