Skip to content

Commit

Permalink
refactor: optimise package size
Browse files Browse the repository at this point in the history
Signed-off-by: Andres Correa Casablanca <[email protected]>
  • Loading branch information
castarco committed Sep 14, 2024
1 parent 34996d7 commit c2ad988
Show file tree
Hide file tree
Showing 50 changed files with 76 additions and 60 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

# testing artifacts
**/tests/playground/*.mjs
**/*-test-artifact.*

# moon
.moon/cache
Expand Down
5 changes: 4 additions & 1 deletion @kindspells/astro-shield/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@
}
},
"imports": {
"#as/*": "./src/*"
"#as/*": {
"types": "./src/*.mts",
"import": "./dist/*.mjs"
}
},
"files": ["dist/*"],
"scripts": {
Expand Down
10 changes: 6 additions & 4 deletions @kindspells/astro-shield/rollup.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,14 @@ const outputBaseConfig = {

export default defineConfig([
{
input: 'src/main.mts',
output: [{ format: 'esm', file: 'dist/main.mjs', sourcemap: true }],
input: 'src/core.mts',
output: [{ format: 'esm', file: 'dist/core.mjs', sourcemap: true }],
...outputBaseConfig,
},
{
input: 'src/core.mts',
output: [{ format: 'esm', file: 'dist/core.mjs', sourcemap: true }],
input: 'src/main.mts',
output: [{ format: 'esm', file: 'dist/main.mjs', sourcemap: true }],
external: ['#as/core'],
...outputBaseConfig,
},
{
Expand All @@ -40,6 +41,7 @@ export default defineConfig([
{
input: 'src/main.mts',
output: [{ format: 'esm', file: 'dist/main.d.mts' }],
external: ['#as/core'],
plugins: [dts()],
},
])
27 changes: 4 additions & 23 deletions @kindspells/astro-shield/src/core.mts
Original file line number Diff line number Diff line change
Expand Up @@ -15,34 +15,15 @@ import type { Plugin } from 'vite'
import { doesFileExist, scanDirectory } from './fs.mts'
import { patchHeaders } from './headers.mts'
import type {
HashesCollection,
Logger,
MiddlewareHashes,
PerPageHashes,
SecurityHeadersOptions,
SRIOptions,
StrictShieldOptions,
} from './types.mts'

export type MiddlewareHashes = {
scripts: Map<string, string>
styles: Map<string, string>
}

export type PerPageHashes = { scripts: Set<string>; styles: Set<string> }
export type PerPageHashesCollection = Map<string, PerPageHashes>

export type HashesCollection = {
inlineScriptHashes: Set<string>
inlineStyleHashes: Set<string>
extScriptHashes: Set<string>
extStyleHashes: Set<string>
perPageSriHashes: PerPageHashesCollection
perResourceSriHashes: MiddlewareHashes
}

export type Logger = {
info(msg: string): void
warn(msg: string): void
error(msg: string): void
}

export type HashesModule = {
[k in keyof HashesCollection]: HashesCollection[k] extends Set<string>
? string[] | undefined
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ import {
it,
} from 'vitest'

import type { HashesModule } from '#as/core.mjs'
import { generateSRIHash } from '#as/core.mjs'
import { doesFileExist } from '#as/fs.mjs'
import type { HashesModule } from '../core.mts'
import { generateSRIHash } from '../core.mts'
import { doesFileExist } from '../fs.mts'

const execFile = promisify(_execFile)

Expand Down
3 changes: 1 addition & 2 deletions @kindspells/astro-shield/src/fs.mts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
import { readdir, stat } from 'node:fs/promises'
import { resolve } from 'node:path'

import type { HashesCollection, Logger } from './core.mts'
import type { SRIOptions } from './types.mts'
import type { HashesCollection, Logger, SRIOptions } from './types.mts'

export const doesFileExist = async (path: string): Promise<boolean> => {
try {
Expand Down
2 changes: 1 addition & 1 deletion @kindspells/astro-shield/src/headers.mts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { PerPageHashes } from './core.mts'
import type { PerPageHashes } from './types.mts'
import type {
CSPDirectives,
CSPOptions,
Expand Down
2 changes: 1 addition & 1 deletion @kindspells/astro-shield/src/main.mts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { fileURLToPath } from 'node:url'

import type { AstroIntegration } from 'astro'

import { getAstroConfigSetup, processStaticFiles } from './core.mts'
import { getAstroConfigSetup, processStaticFiles } from '#as/core'
import type { ShieldOptions, SRIOptions } from './types.mts'

type AstroHooks = AstroIntegration['hooks']
Expand Down
2 changes: 1 addition & 1 deletion @kindspells/astro-shield/src/state.mts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* SPDX-License-Identifier: MIT
*/

import type { MiddlewareHashes } from './core.mts'
import type { MiddlewareHashes } from './types.mts'

let globalHashes: MiddlewareHashes

Expand Down
10 changes: 5 additions & 5 deletions @kindspells/astro-shield/src/tests/core.test.mts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ import {
sriHashesEqual,
updateDynamicPageSriHashes,
updateStaticPageSriHashes,
} from '#as/core.mts'
import { doesFileExist } from '#as/fs.mts'
} from '../core.mts'
import { doesFileExist } from '../fs.mts'

type SriHashes = {
scripts: Record<string, string>
Expand Down Expand Up @@ -615,7 +615,7 @@ describe('updateStaticPageSriHashes', () => {
<title>My Test Page</title>
</head>
<body>
<script type="module" src="/core.mjs"></script>
<script type="module" src="/state.mjs"></script>
</body>
</html>`

Expand All @@ -624,7 +624,7 @@ describe('updateStaticPageSriHashes', () => {
<title>My Test Page</title>
</head>
<body>
<script type="module" src="/core.mjs" integrity="sha256-Pl+u7V7n6H0yGnuW+XJq9vAtnskDhX+uuHdOPF/MuEU="></script>
<script type="module" src="/state.mjs" integrity="sha256-GnAS2y0U5qIXDOpQOY7sE+D1/ncp19EQxwySQzSR/bQ="></script>
</body>
</html>`

Expand All @@ -642,7 +642,7 @@ describe('updateStaticPageSriHashes', () => {

expect(
h.extScriptHashes.has(
'sha256-Pl+u7V7n6H0yGnuW+XJq9vAtnskDhX+uuHdOPF/MuEU=',
'sha256-GnAS2y0U5qIXDOpQOY7sE+D1/ncp19EQxwySQzSR/bQ=',
),
).toBe(true)
expect(h.inlineScriptHashes.size).toBe(0)
Expand Down
6 changes: 3 additions & 3 deletions @kindspells/astro-shield/src/tests/fs.test.mts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import { relative, resolve } from 'node:path'

import { describe, expect, it } from 'vitest'

import type { HashesCollection } from '#as/core.mts'
import { generateSRIHash } from '#as/core.mts'
import { doesFileExist, scanDirectory } from '#as/fs.mts'
import type { HashesCollection } from '../types.mts'
import { generateSRIHash } from '../core.mts'
import { doesFileExist, scanDirectory } from '../fs.mts'

const testsDir = new URL('.', import.meta.url).pathname

Expand Down
4 changes: 2 additions & 2 deletions @kindspells/astro-shield/src/tests/headers.test.mts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import {
serialiseCspDirectives,
serialiseHashes,
setSrcDirective,
} from '#as/headers.mjs'
import type { CSPDirectives, SecurityHeadersOptions } from '#as/types.mts'
} from '../headers.mjs'
import type { CSPDirectives, SecurityHeadersOptions } from '../types.mts'

describe('serialiseHashes', () => {
it('returns an empty string for an empty set', () => {
Expand Down
2 changes: 1 addition & 1 deletion @kindspells/astro-shield/src/tests/main.test.mts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import type { AstroIntegration } from 'astro'
import { describe, expect, it } from 'vitest'

import defaultIntegrationExport, { shield } from '#as/main.mts'
import defaultIntegrationExport, { shield } from '../main.mts'

describe('sriCSP', () => {
it('is exported as default', () => {
Expand Down
2 changes: 1 addition & 1 deletion @kindspells/astro-shield/src/tests/state.test.mts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import { describe, expect, it } from 'vitest'

import { getGlobalHashes } from '#as/state.mts'
import { getGlobalHashes } from '../state.mts'

describe('getGlobalHashes', () => {
it('returns a singleton', () => {
Expand Down
30 changes: 30 additions & 0 deletions @kindspells/astro-shield/src/types.mts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,14 @@ export type SRIOptions = {
stylesAllowListUrls?: string[]
}

type VercelConfig = {
provider: 'vercel'
configFile: string // TODO: auto-discovery?
}

export type SecurityHeadersOptions = {
enableOnStaticPages?: VercelConfig | undefined

/**
* - If set, it controls how the CSP (Content Security Policy) header will be
* generated in the middleware.
Expand Down Expand Up @@ -151,3 +158,26 @@ export type StrictShieldOptions = ShieldOptions & {
distDir: string
sri: SRIOptions & { enableStatic: boolean; enableMiddleware: boolean }
}

export type Logger = {
info(msg: string): void
warn(msg: string): void
error(msg: string): void
}

export type MiddlewareHashes = {
scripts: Map<string, string>
styles: Map<string, string>
}

export type PerPageHashes = { scripts: Set<string>; styles: Set<string> }
export type PerPageHashesCollection = Map<string, PerPageHashes>

export type HashesCollection = {
inlineScriptHashes: Set<string>
inlineStyleHashes: Set<string>
extScriptHashes: Set<string>
extStyleHashes: Set<string>
perPageSriHashes: PerPageHashesCollection
perResourceSriHashes: MiddlewareHashes
}
2 changes: 1 addition & 1 deletion @kindspells/astro-shield/vitest.config.e2e.mts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ import { defineConfig } from 'vitest/config'

export default defineConfig({
test: {
include: ['e2e/**/*.test.mts'],
include: ['src/e2e/**/*.test.mts'],
},
})
20 changes: 10 additions & 10 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pnpm-workspace.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
packages:
- 'docs'
- '@kindspells/*'
- '@kindspells/astro-shield/e2e/fixtures/*'
- '@kindspells/astro-shield/src/e2e/fixtures/*'

0 comments on commit c2ad988

Please sign in to comment.