Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

build: implement eslint and prettier #53

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ insert_final_newline = true

[*.js]
charset = utf-8
indent_style = tab
indent_style = space
indent_size = 2

[*.yml]
indent_style = space
Expand Down
30 changes: 30 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// @ts-check

/**
* @type {import('eslint').Linter.Config}
**/
const eslintConfig = {
root: true,
parser: '@typescript-eslint/parser',
plugins: ['@typescript-eslint', 'prettier'],
extends: [
'eslint:recommended',
'plugin:@typescript-eslint/recommended',
'prettier',
],
rules: {
'@typescript-eslint/no-unused-vars': [
'error',
{
varsIgnorePattern: '^_',
argsIgnorePattern: '^_',
destructuredArrayIgnorePattern: '^_',
},
],
'prettier/prettier': 'error',
},
ignorePatterns: ['dist/*'],
}

// eslint-disable-next-line no-undef
module.exports = eslintConfig
12 changes: 12 additions & 0 deletions .prettierrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// @ts-check

/**
* @type {import('prettier').Options}
**/
const prettierConfig = {
semi: false,
singleQuote: true,
jsxSingleQuote: true,
}

module.exports = prettierConfig
114 changes: 57 additions & 57 deletions __tests__/buildCSPHeaders.ts
Original file line number Diff line number Diff line change
@@ -1,62 +1,62 @@
// Local imports
import buildCSPHeaders from '../src/lib/buildCSPHeaders'
import { buildCSPHeaders } from '../src/lib/buildCSPHeaders'
import { CSP_REQUIRED_DIRECTIVE_DEFAULTS } from '../src/models/CSP'
import type { NextSafeConfig } from '../src/models/nextSafe'
import { type NextSafeConfig } from '../src/models/nextSafe'

describe('buildCSPHeaders', () => {
function testCSPWithConfig(testName: string, config?: NextSafeConfig) {
test(testName, () => {
const builtCSPHeaders = buildCSPHeaders(config)
const expectedCSP = {
...CSP_REQUIRED_DIRECTIVE_DEFAULTS,
...(config?.contentSecurityPolicy || {}),
}

if (config?.isDev && config.contentSecurityPolicy !== false) {
if (!config.contentSecurityPolicy?.['connect-src']) {
expectedCSP['connect-src'] = '\'self\' webpack://*'
}

if (!config.contentSecurityPolicy?.['script-src']) {
expectedCSP['script-src'] = '\'self\' \'unsafe-eval\''
}

if (!config.contentSecurityPolicy?.['style-src']) {
expectedCSP['style-src'] = '\'self\' \'unsafe-inline\''
}
}

expect(builtCSPHeaders).toBeInstanceOf(Array)
expect(builtCSPHeaders).toHaveLength(3)

builtCSPHeaders.forEach(header => {
Object.entries(expectedCSP).forEach(([key, value]) => {
expect(header.value).toContain(`${key} ${value};`)
})
})
})
}

describe('production mode', () => {
testCSPWithConfig('with defaults')

testCSPWithConfig('with customized config', {
contentSecurityPolicy: {
'base-uri': 'https://trezy.com',
},
})
})

describe('development mode', () => {
testCSPWithConfig('with defaults', {
isDev: true,
})

testCSPWithConfig('with customized config', {
contentSecurityPolicy: {
'base-uri': 'https://trezy.com',
},
isDev: true,
})
})
function testCSPWithConfig(testName: string, config?: NextSafeConfig) {
test(testName, () => {
const builtCSPHeaders = buildCSPHeaders(config)
const expectedCSP = {
...CSP_REQUIRED_DIRECTIVE_DEFAULTS,
...(config?.contentSecurityPolicy || {}),
}

if (config?.isDev && config.contentSecurityPolicy !== false) {
if (!config.contentSecurityPolicy?.['connect-src']) {
expectedCSP['connect-src'] = "'self' webpack://*"
}

if (!config.contentSecurityPolicy?.['script-src']) {
expectedCSP['script-src'] = "'self' 'unsafe-eval'"
}

if (!config.contentSecurityPolicy?.['style-src']) {
expectedCSP['style-src'] = "'self' 'unsafe-inline'"
}
}

expect(builtCSPHeaders).toBeInstanceOf(Array)
expect(builtCSPHeaders).toHaveLength(3)

builtCSPHeaders.forEach((header) => {
Object.entries(expectedCSP).forEach(([key, value]) => {
expect(header.value).toContain(`${key} ${value};`)
})
})
})
}

describe('production mode', () => {
testCSPWithConfig('with defaults')

testCSPWithConfig('with customized config', {
contentSecurityPolicy: {
'base-uri': 'https://trezy.com',
},
})
})

describe('development mode', () => {
testCSPWithConfig('with defaults', {
isDev: true,
})

testCSPWithConfig('with customized config', {
contentSecurityPolicy: {
'base-uri': 'https://trezy.com',
},
isDev: true,
})
})
})
78 changes: 41 additions & 37 deletions __tests__/buildPermissionsPolicyHeaders.ts
Original file line number Diff line number Diff line change
@@ -1,44 +1,48 @@
// Local imports
import buildPermissionsPolicyHeaders from '../src/lib/buildPermissionsPolicyHeaders'
import { buildPermissionsPolicyHeaders } from '../src/lib/buildPermissionsPolicyHeaders'

describe('buildPermissionsPolicyHeaders', () => {
test('with defaults', () => {
const result = buildPermissionsPolicyHeaders()
test('with defaults', () => {
const result = buildPermissionsPolicyHeaders()

expect(result).toEqual(
[
{
key: 'Feature-Policy',
value: "clipboard-read 'none';clipboard-write 'none';gamepad 'none';speaker-selection 'none';accelerometer 'none';ambient-light-sensor 'none';autoplay 'none';battery 'none';camera 'none';cross-origin-isolated 'none';display-capture 'none';document-domain 'none';encrypted-media 'none';execution-while-not-rendered 'none';execution-while-out-of-viewport 'none';fullscreen 'none';geolocation 'none';gyroscope 'none';magnetometer 'none';microphone 'none';midi 'none';navigation-override 'none';payment 'none';picture-in-picture 'none';publickey-credentials-get 'none';screen-wake-lock 'none';sync-xhr 'none';usb 'none';web-share 'none';xr-spatial-tracking 'none';"
},
{
key: 'Permissions-Policy',
value: 'clipboard-read=(),clipboard-write=(),gamepad=(),speaker-selection=(),accelerometer=(),ambient-light-sensor=(),autoplay=(),battery=(),camera=(),cross-origin-isolated=(),display-capture=(),document-domain=(),encrypted-media=(),execution-while-not-rendered=(),execution-while-out-of-viewport=(),fullscreen=(),geolocation=(),gyroscope=(),magnetometer=(),microphone=(),midi=(),navigation-override=(),payment=(),picture-in-picture=(),publickey-credentials-get=(),screen-wake-lock=(),sync-xhr=(),usb=(),web-share=(),xr-spatial-tracking=()'
}
]
)
})
expect(result).toEqual([
{
key: 'Feature-Policy',
value:
"clipboard-read 'none';clipboard-write 'none';gamepad 'none';speaker-selection 'none';accelerometer 'none';ambient-light-sensor 'none';autoplay 'none';battery 'none';camera 'none';cross-origin-isolated 'none';display-capture 'none';document-domain 'none';encrypted-media 'none';execution-while-not-rendered 'none';execution-while-out-of-viewport 'none';fullscreen 'none';geolocation 'none';gyroscope 'none';magnetometer 'none';microphone 'none';midi 'none';navigation-override 'none';payment 'none';picture-in-picture 'none';publickey-credentials-get 'none';screen-wake-lock 'none';sync-xhr 'none';usb 'none';web-share 'none';xr-spatial-tracking 'none';",
},
{
key: 'Permissions-Policy',
value:
'clipboard-read=(),clipboard-write=(),gamepad=(),speaker-selection=(),accelerometer=(),ambient-light-sensor=(),autoplay=(),battery=(),camera=(),cross-origin-isolated=(),display-capture=(),document-domain=(),encrypted-media=(),execution-while-not-rendered=(),execution-while-out-of-viewport=(),fullscreen=(),geolocation=(),gyroscope=(),magnetometer=(),microphone=(),midi=(),navigation-override=(),payment=(),picture-in-picture=(),publickey-credentials-get=(),screen-wake-lock=(),sync-xhr=(),usb=(),web-share=(),xr-spatial-tracking=()',
},
])
})

test('with custom config', () => {
const result = buildPermissionsPolicyHeaders({
permissionsPolicy: {
battery: 'https://trezy.dev',
gyroscope: '*'
},
permissionsPolicyDirectiveSupport: ["proposed", "standard", "experimental"],
})
test('with custom config', () => {
const result = buildPermissionsPolicyHeaders({
permissionsPolicy: {
battery: 'https://trezy.dev',
gyroscope: '*',
},
permissionsPolicyDirectiveSupport: [
'proposed',
'standard',
'experimental',
],
})

expect(result).toEqual(
[
{
key: 'Feature-Policy',
value: "clipboard-read 'none';clipboard-write 'none';gamepad 'none';speaker-selection 'none';accelerometer 'none';ambient-light-sensor 'none';autoplay 'none';battery https://trezy.dev;camera 'none';cross-origin-isolated 'none';display-capture 'none';document-domain 'none';encrypted-media 'none';execution-while-not-rendered 'none';execution-while-out-of-viewport 'none';fullscreen 'none';geolocation 'none';gyroscope *;magnetometer 'none';microphone 'none';midi 'none';navigation-override 'none';payment 'none';picture-in-picture 'none';publickey-credentials-get 'none';screen-wake-lock 'none';sync-xhr 'none';usb 'none';web-share 'none';xr-spatial-tracking 'none';conversion-measurement 'none';focus-without-user-activation 'none';hid 'none';idle-detection 'none';serial 'none';sync-script 'none';trust-token-redemption 'none';vertical-scroll 'none';"
},
{
key: 'Permissions-Policy',
value: 'clipboard-read=(),clipboard-write=(),gamepad=(),speaker-selection=(),accelerometer=(),ambient-light-sensor=(),autoplay=(),battery=(https://trezy.dev),camera=(),cross-origin-isolated=(),display-capture=(),document-domain=(),encrypted-media=(),execution-while-not-rendered=(),execution-while-out-of-viewport=(),fullscreen=(),geolocation=(),gyroscope=(*),magnetometer=(),microphone=(),midi=(),navigation-override=(),payment=(),picture-in-picture=(),publickey-credentials-get=(),screen-wake-lock=(),sync-xhr=(),usb=(),web-share=(),xr-spatial-tracking=(),conversion-measurement=(),focus-without-user-activation=(),hid=(),idle-detection=(),serial=(),sync-script=(),trust-token-redemption=(),vertical-scroll=()'
}
]
)
})
expect(result).toEqual([
{
key: 'Feature-Policy',
value:
"clipboard-read 'none';clipboard-write 'none';gamepad 'none';speaker-selection 'none';accelerometer 'none';ambient-light-sensor 'none';autoplay 'none';battery https://trezy.dev;camera 'none';cross-origin-isolated 'none';display-capture 'none';document-domain 'none';encrypted-media 'none';execution-while-not-rendered 'none';execution-while-out-of-viewport 'none';fullscreen 'none';geolocation 'none';gyroscope *;magnetometer 'none';microphone 'none';midi 'none';navigation-override 'none';payment 'none';picture-in-picture 'none';publickey-credentials-get 'none';screen-wake-lock 'none';sync-xhr 'none';usb 'none';web-share 'none';xr-spatial-tracking 'none';conversion-measurement 'none';focus-without-user-activation 'none';hid 'none';idle-detection 'none';serial 'none';sync-script 'none';trust-token-redemption 'none';vertical-scroll 'none';",
},
{
key: 'Permissions-Policy',
value:
'clipboard-read=(),clipboard-write=(),gamepad=(),speaker-selection=(),accelerometer=(),ambient-light-sensor=(),autoplay=(),battery=(https://trezy.dev),camera=(),cross-origin-isolated=(),display-capture=(),document-domain=(),encrypted-media=(),execution-while-not-rendered=(),execution-while-out-of-viewport=(),fullscreen=(),geolocation=(),gyroscope=(*),magnetometer=(),microphone=(),midi=(),navigation-override=(),payment=(),picture-in-picture=(),publickey-credentials-get=(),screen-wake-lock=(),sync-xhr=(),usb=(),web-share=(),xr-spatial-tracking=(),conversion-measurement=(),focus-without-user-activation=(),hid=(),idle-detection=(),serial=(),sync-script=(),trust-token-redemption=(),vertical-scroll=()',
},
])
})
})
78 changes: 38 additions & 40 deletions __tests__/crunchCSPHeaderValue.ts
Original file line number Diff line number Diff line change
@@ -1,44 +1,42 @@
// Local imports
import crunchCSPHeaderValue from '../src/lib/crunchCSPHeaderValue'
import { crunchCSPHeaderValue } from '../src/lib/crunchCSPHeaderValue'

describe('crunchCSPHeaderValue', () => {
test('with single header', () => {
const crunchedHeaders = crunchCSPHeaderValue({
'base-uri': 'header-value',
})

expect(crunchedHeaders).toEqual('base-uri header-value;')
})

test('with multiple headers', () => {
const crunchedHeaders = crunchCSPHeaderValue({
'base-uri': 'header-value1',
'child-src': 'header-value2',
})

expect(crunchedHeaders).toEqual('base-uri header-value1;child-src header-value2;')
})

test('with values array', () => {
const crunchedHeaders = crunchCSPHeaderValue({
'base-uri': [
'header-value1',
'header-value2',
],
})

expect(crunchedHeaders).toEqual('base-uri header-value1 header-value2;')
})

test('with mixed value types', () => {
const crunchedHeaders = crunchCSPHeaderValue({
'base-uri': 'header-value1',
'child-src': [
'header-value2',
'header-value3',
],
})

expect(crunchedHeaders).toEqual('base-uri header-value1;child-src header-value2 header-value3;')
})
test('with single header', () => {
const crunchedHeaders = crunchCSPHeaderValue({
'base-uri': 'header-value',
})

expect(crunchedHeaders).toEqual('base-uri header-value;')
})

test('with multiple headers', () => {
const crunchedHeaders = crunchCSPHeaderValue({
'base-uri': 'header-value1',
'child-src': 'header-value2',
})

expect(crunchedHeaders).toEqual(
'base-uri header-value1;child-src header-value2;'
)
})

test('with values array', () => {
const crunchedHeaders = crunchCSPHeaderValue({
'base-uri': ['header-value1', 'header-value2'],
})

expect(crunchedHeaders).toEqual('base-uri header-value1 header-value2;')
})

test('with mixed value types', () => {
const crunchedHeaders = crunchCSPHeaderValue({
'base-uri': 'header-value1',
'child-src': ['header-value2', 'header-value3'],
})

expect(crunchedHeaders).toEqual(
'base-uri header-value1;child-src header-value2 header-value3;'
)
})
})
Loading