Skip to content

Commit

Permalink
Merge pull request #25 from niieani/fix-esm
Browse files Browse the repository at this point in the history
feat: fix esm build
  • Loading branch information
niieani authored Nov 9, 2023
2 parents dd5db24 + ff280b0 commit 26ba6d2
Show file tree
Hide file tree
Showing 13 changed files with 7,937 additions and 8,043 deletions.
9 changes: 9 additions & 0 deletions .config/beemo/eslint.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { ESLintConfig } from '@beemo/driver-eslint'

const config: ESLintConfig = {
rules: {
'@typescript-eslint/lines-between-class-members': 'off',
},
}

export default config
7 changes: 7 additions & 0 deletions .config/beemo/jest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import type jest from '@niieani/scaffold/src/configs/jest'

export default {
moduleNameMapper: {
'^(\\.\\.?\\/.+)\\.jsx?$': '$1',
},
} as typeof jest
5 changes: 5 additions & 0 deletions .config/beemo/typescript.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import type TypeScript from '@niieani/scaffold/src/configs/typescript'

export default {
compilerOptions: {},
} as typeof TypeScript
1 change: 1 addition & 0 deletions .prototools
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node = "21.1.0"
546 changes: 0 additions & 546 deletions .yarn/plugins/@yarnpkg/plugin-interactive-tools.cjs

This file was deleted.

9 changes: 0 additions & 9 deletions .yarn/plugins/@yarnpkg/plugin-typescript.cjs

This file was deleted.

873 changes: 0 additions & 873 deletions .yarn/releases/yarn-3.5.0.cjs

This file was deleted.

893 changes: 893 additions & 0 deletions .yarn/releases/yarn-4.0.1.cjs

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions .yarnrc.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
compressionLevel: mixed

enableGlobalCache: false

nodeLinker: node-modules

plugins:
- path: .yarn/plugins/@yarnpkg/plugin-interactive-tools.cjs
spec: "@yarnpkg/plugin-interactive-tools"
- path: .yarn/plugins/@yarnpkg/plugin-typescript.cjs
spec: "@yarnpkg/plugin-typescript"
- path: .yarn/plugins/@yarnpkg/plugin-postinstall-dev.cjs
spec: "https://raw.githubusercontent.com/sachinraja/yarn-plugin-postinstall-dev/main/bundles/%40yarnpkg/plugin-postinstall-dev.js"

yarnPath: .yarn/releases/yarn-3.5.0.cjs
yarnPath: .yarn/releases/yarn-4.0.1.cjs
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,10 @@
"tagFormat": "${version}"
},
"devDependencies": {
"@niieani/scaffold": "^1.6.7",
"get-port": "^5.1.1",
"puppeteer": "^19.11.1",
"puppeteer-core": "^19.11.1"
"@niieani/scaffold": "^1.7.11",
"get-port": "^5.0.0",
"puppeteer": "^21.5.0",
"puppeteer-core": "^21.5.0"
},
"packageManager": "[email protected]",
"publishConfig": {
Expand Down
57 changes: 32 additions & 25 deletions src/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,23 @@ import { RequestInterceptionManager } from './main'
const puppeteer = _puppeteer as unknown as typeof puppeteerType

let server: Server
let port = 3_000
let browser: puppeteerType.Browser
let page: puppeteerType.Page
let client: puppeteerType.CDPSession
let browserClient: puppeteerType.CDPSession
let manager: RequestInterceptionManager

const host = 'localhost'
let port = 3_000
const serverHandlerRef: { current?: http.RequestListener } = {}

const startServer = async (handler: http.RequestListener): Promise<void> => {
// eslint-disable-next-line require-atomic-updates
port = await getPort({ host, port })
const startServer = async (): Promise<{ server: Server; port: number }> => {
const usedPort = await getPort({ host, port })
return new Promise((resolve) => {
server = http.createServer(handler).listen(port, host, resolve)
const s = http.createServer((req, res) => {
serverHandlerRef.current?.(req, res)
})
s.listen(usedPort, host, () => void resolve({ server: s, port: usedPort }))
})
}

Expand All @@ -32,17 +35,20 @@ const stopServer = (): Promise<void> =>
if (err) reject(err)
else resolve()
})
server.closeAllConnections()
})

describe('RequestInterceptionManager', () => {
jest.setTimeout(5_000)

beforeAll(async () => {
browser = await puppeteer.launch()
browser = await puppeteer.launch({ headless: 'new' })
browserClient = await browser.target().createCDPSession()
;({ port, server } = await startServer())
})

afterAll(async () => {
await stopServer()
await browser.close()
})

Expand All @@ -53,7 +59,6 @@ describe('RequestInterceptionManager', () => {

afterEach(async () => {
await page.close()
await stopServer()
await manager.clear()
})

Expand All @@ -65,10 +70,10 @@ describe('RequestInterceptionManager', () => {
'with streamResponse: $streamResponse',
(options: { streamResponse: boolean }) => {
it('should intercept and modify response', async () => {
await startServer((req, res) => {
serverHandlerRef.current = (req, res) => {
res.writeHead(200, { 'Content-Type': 'text/plain' })
res.end('Hello, world!')
})
}

manager = new RequestInterceptionManager(client)
await manager.intercept({
Expand All @@ -92,7 +97,7 @@ describe('RequestInterceptionManager', () => {
})

it('should intercept and modify request', async () => {
await startServer((req, res) => {
serverHandlerRef.current = (req, res) => {
if (req.url === '/') {
res.writeHead(200, { 'Content-Type': 'text/plain' })
res.end('Original request')
Expand All @@ -103,7 +108,7 @@ describe('RequestInterceptionManager', () => {
res.writeHead(404, { 'Content-Type': 'text/plain' })
res.end('Not found')
}
})
}

manager = new RequestInterceptionManager(client)
await manager.intercept({
Expand All @@ -127,10 +132,10 @@ describe('RequestInterceptionManager', () => {
})

it('should not intercept requests not matching urlPattern', async () => {
await startServer((req, res) => {
serverHandlerRef.current = (req, res) => {
res.writeHead(200, { 'Content-Type': 'text/plain' })
res.end('Hello, world!')
})
}

manager = new RequestInterceptionManager(client)
await manager.intercept({
Expand All @@ -154,10 +159,10 @@ describe('RequestInterceptionManager', () => {
})

it('should handle intercepting a 304 response', async () => {
await startServer((req, res) => {
serverHandlerRef.current = (req, res) => {
res.writeHead(304, { 'Content-Type': 'text/plain' })
res.end()
})
}

manager = new RequestInterceptionManager(client)
await manager.intercept({
Expand All @@ -182,7 +187,7 @@ describe('RequestInterceptionManager', () => {
})

it('should handle intercepting a redirect response', async () => {
await startServer((req, res) => {
serverHandlerRef.current = (req, res) => {
if (req.url === '/redirected') {
res.writeHead(200, { 'Content-Type': 'text/plain' })
res.end('Works')
Expand All @@ -192,7 +197,7 @@ describe('RequestInterceptionManager', () => {
})
res.end()
}
})
}

manager = new RequestInterceptionManager(client)
await manager.intercept({
Expand Down Expand Up @@ -237,10 +242,10 @@ describe('RequestInterceptionManager', () => {
})

it('should handle different status codes', async () => {
await startServer((req, res) => {
serverHandlerRef.current = (req, res) => {
res.writeHead(403, { 'Content-Type': 'text/plain' })
res.end('It is forbidden')
})
}

manager = new RequestInterceptionManager(client)
await manager.intercept({
Expand Down Expand Up @@ -269,7 +274,7 @@ describe('RequestInterceptionManager', () => {
})

it('should handle multiple interception rules', async () => {
await startServer((req, res) => {
serverHandlerRef.current = (req, res) => {
if (req.url === '/first') {
res.writeHead(200, { 'Content-Type': 'text/plain' })
res.end('First')
Expand All @@ -280,7 +285,7 @@ describe('RequestInterceptionManager', () => {
res.writeHead(404, { 'Content-Type': 'text/plain' })
res.end('Not found')
}
})
}

manager = new RequestInterceptionManager(client)
await manager.intercept(
Expand Down Expand Up @@ -319,7 +324,7 @@ describe('RequestInterceptionManager', () => {
})

it('should intercept and modify requests on new tabs', async () => {
await startServer((req, res) => {
serverHandlerRef.current = (req, res) => {
if (req.url === '/') {
res.writeHead(200, { 'Content-Type': 'text/html' })
res.end('<a href="/new-tab" target="_blank">Open new tab</a>')
Expand All @@ -330,7 +335,7 @@ describe('RequestInterceptionManager', () => {
res.writeHead(404, { 'Content-Type': 'text/plain' })
res.end('Not found')
}
})
}

// Set up request interception for the initial page
manager = new RequestInterceptionManager(browserClient)
Expand Down Expand Up @@ -372,6 +377,8 @@ describe('RequestInterceptionManager', () => {
return response.text()
}, `http://localhost:${port}/new-tab`)

await newPage.close()

expect(newResponse).toBe('Hello, new tab intercepted!')
})
},
Expand All @@ -382,7 +389,7 @@ describe('RequestInterceptionManager', () => {
let messageId = 0

// Set up an EventStream server that sends a series of messages
await startServer((req, res) => {
serverHandlerRef.current = (req, res) => {
if (req.url === '/') {
res.writeHead(200, { 'Content-Type': 'text/plain' })
res.end('Hello!')
Expand All @@ -408,7 +415,7 @@ describe('RequestInterceptionManager', () => {
sendNextMessage()
sendNextMessage()
sendNextMessage()
})
}

manager = new RequestInterceptionManager(client)
await manager.intercept({
Expand Down
3 changes: 2 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
/* eslint-disable no-continue,no-await-in-loop,node/no-unpublished-import */
import { promisify } from 'util'
import type { CDPSession, Protocol } from 'puppeteer-core'
import { getUrlPatternRegExp } from './urlPattern'
// eslint-disable-next-line import/no-unresolved
import { getUrlPatternRegExp } from './urlPattern.js'

export { getUrlPatternRegExp }

Expand Down
Loading

0 comments on commit 26ba6d2

Please sign in to comment.