Skip to content

Commit

Permalink
fix(dependencies): Update dependencies. Use chokidar instead of @parc…
Browse files Browse the repository at this point in the history
…el/watcher for hotloading
  • Loading branch information
vinsonchuong committed Sep 22, 2021
1 parent 7dc6466 commit 33d4d5e
Show file tree
Hide file tree
Showing 19 changed files with 1,847 additions and 3,576 deletions.
3 changes: 2 additions & 1 deletion bin.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/env node
import process from 'node:process'
import {createRequire} from 'node:module'
import run from 'puff-pastry'

Expand All @@ -9,6 +10,6 @@ if (process.env.NODE_ENV === 'production') {
run('./cli/index.js')
} else {
run('./cli/index.js', {
flags: ['--loader', hotEsmPath, '--no-warnings']
flags: ['--loader', hotEsmPath, '--no-warnings'],
})
}
41 changes: 21 additions & 20 deletions bin.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import process from 'node:process'
import test from 'ava'
import {useTemporaryDirectory, runProcess, wait} from 'ava-patterns'
import install from 'quick-install'
Expand All @@ -20,13 +21,13 @@ test('starting a development server with hot-reloading', async (t) => {
body: 'Hello World!'
}
}
`
`,
)

const server = runProcess(t, {
command: ['npx', 'pass-notes', 'server.mjs'],
cwd: directory.path,
env: {PORT: '10010'}
env: {PORT: '10010'},
})
try {
await server.waitForOutput('Listening', 5000)
Expand All @@ -39,15 +40,15 @@ test('starting a development server with hot-reloading', async (t) => {
await sendRequest({
method: 'GET',
url: 'http://localhost:10010',
headers: {}
headers: {},
}),
{
status: 200,
headers: {
'content-type': 'text/plain'
'content-type': 'text/plain',
},
body: 'Hello World!'
}
body: 'Hello World!',
},
)

await directory.writeFile(
Expand All @@ -62,23 +63,23 @@ test('starting a development server with hot-reloading', async (t) => {
body: 'Hello There!'
}
}
`
`,
)
await wait(500)

t.like(
await sendRequest({
method: 'GET',
url: 'http://localhost:10010',
headers: {}
headers: {},
}),
{
status: 200,
headers: {
'content-type': 'text/plain'
'content-type': 'text/plain',
},
body: 'Hello There!'
}
body: 'Hello There!',
},
)
})

Expand All @@ -99,13 +100,13 @@ test('not enabling hot-reloading in production', async (t) => {
body: 'Hello World!'
}
}
`
`,
)

const server = runProcess(t, {
command: ['npx', 'pass-notes', 'server.mjs'],
cwd: directory.path,
env: {PORT: '10011', NODE_ENV: 'production'}
env: {PORT: '10011', NODE_ENV: 'production'},
})
try {
await server.waitForOutput('Listening', 5000)
Expand All @@ -118,11 +119,11 @@ test('not enabling hot-reloading in production', async (t) => {
await sendRequest({
method: 'GET',
url: 'http://localhost:10011',
headers: {}
headers: {},
}),
{
body: 'Hello World!'
}
body: 'Hello World!',
},
)

await directory.writeFile(
Expand All @@ -137,18 +138,18 @@ test('not enabling hot-reloading in production', async (t) => {
body: 'Hello There!'
}
}
`
`,
)
await wait(500)

t.like(
await sendRequest({
method: 'GET',
url: 'http://localhost:10011',
headers: {}
headers: {},
}),
{
body: 'Hello World!'
}
body: 'Hello World!',
},
)
})
2 changes: 1 addition & 1 deletion cli/find-open-port.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ import getPort from 'get-port'

export default function (...portsToTry) {
return getPort({
port: portsToTry.filter((n) => typeof n === 'number' && !Number.isNaN(n))
port: portsToTry.filter((n) => typeof n === 'number' && !Number.isNaN(n)),
})
}
10 changes: 5 additions & 5 deletions cli/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export default async function ({cwd, env, argv, stdout}) {
const finish = logger.measure({
level: 'INFO',
topic: 'HTTP',
message: `${request.method} ${request.url}`
message: `${request.method} ${request.url}`,
})

try {
Expand All @@ -60,17 +60,17 @@ export default async function ({cwd, env, argv, stdout}) {
response = {
status: 500,
headers: {
'Content-Type': 'text/plain'
'Content-Type': 'text/plain',
},
body: error_.message
body: error_.message,
}
error = error_
}

finish({
level: error ? 'ERROR' : 'INFO',
message: `${response.status}`,
error
error,
})

return response
Expand All @@ -79,7 +79,7 @@ export default async function ({cwd, env, argv, stdout}) {
logger.log({
level: 'INFO',
topic: 'HTTP',
message: `Listening on port ${port}`
message: `Listening on port ${port}`,
})

return server
Expand Down
53 changes: 27 additions & 26 deletions cli/index.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import process from 'node:process'
import * as https from 'node:https'
import test from 'ava'
import {useTemporaryDirectory} from 'ava-patterns'
Expand All @@ -21,8 +22,8 @@ async function runScript(t, env, script) {
stdout: {
write(text) {
output.push(text)
}
}
},
},
})
t.teardown(async () => {
await stopServer(server)
Expand All @@ -46,7 +47,7 @@ test('running the CLI', async (t) => {
body: 'Hello World!'
}
}
`
`,
)

t.true(output[0].includes('Listening on port 11000'))
Expand All @@ -55,15 +56,15 @@ test('running the CLI', async (t) => {
await sendRequest({
method: 'GET',
url: 'http://localhost:11000',
headers: {}
headers: {},
}),
{
status: 200,
headers: {
'content-type': 'text/plain'
'content-type': 'text/plain',
},
body: 'Hello World!'
}
body: 'Hello World!',
},
)
})

Expand All @@ -79,7 +80,7 @@ test('defaulting to port 8080', async (t) => {
body: 'Hello World!'
}
}
`
`,
)

t.true(output[0].includes('Listening on port 8080'))
Expand All @@ -88,9 +89,9 @@ test('defaulting to port 8080', async (t) => {
await sendRequest({
method: 'GET',
url: 'http://localhost:8080',
headers: {}
headers: {},
}),
{status: 200}
{status: 200},
)
})

Expand All @@ -102,19 +103,19 @@ test('gracefully handling errors', async (t) => {
export default function () {
throw new Error('Something bad happened.')
}
`
`,
)

t.like(
await sendRequest({
method: 'GET',
url: 'http://localhost:11001',
headers: {}
headers: {},
}),
{
status: 500,
body: 'Something bad happened.'
}
body: 'Something bad happened.',
},
)
})

Expand All @@ -140,11 +141,11 @@ test('automatically creating a self-signed certificate for localhost', async (t)
body: 'Hello World!'
}
}
`
`,
)

t.like(await getCertificate('https://localhost:11002'), {
subject: {CN: 'localhost'}
subject: {CN: 'localhost'},
})
})

Expand All @@ -162,11 +163,11 @@ test('allowing a certificate to be provided via environment variables', async (t
body: 'Hello World!'
}
}
`
`,
)

t.like(await getCertificate('https://localhost:11003'), {
subject: {CN: 'foo'}
subject: {CN: 'foo'},
})
})

Expand All @@ -189,11 +190,11 @@ test('allowing a certificate to be provided in application code', async (t) => {
body: 'Hello World!'
}
}
`
`,
)

t.like(await getCertificate('https://localhost:11004'), {
subject: {CN: 'bar'}
subject: {CN: 'bar'},
})
})

Expand All @@ -215,13 +216,13 @@ test('logging requests', async (t) => {
body: 'Hello World!'
}
}
`
`,
)

await sendRequest({
method: 'GET',
url: 'http://localhost:11005/foo',
headers: {}
headers: {},
})
t.true(output[1].includes('[INFO] [HTTP] GET /foo'))
t.true(output[2].includes('[INFO] [HTTP] GET /foo › 200'))
Expand All @@ -230,15 +231,15 @@ test('logging requests', async (t) => {
method: 'POST',
url: 'http://localhost:11005/bar',
headers: {},
body: ''
body: '',
})
t.true(output[3].includes('[INFO] [HTTP] POST /bar'))
t.true(output[4].includes('[INFO] [HTTP] POST /bar › 200'))

await sendRequest({
method: 'GET',
url: 'http://localhost:11005/error',
headers: {}
headers: {},
})
t.true(output[5].includes('[INFO] [HTTP] GET /error'))
t.true(output[6].includes('[ERROR] [HTTP] GET /error › 500'))
Expand All @@ -258,13 +259,13 @@ test('using a custom logger', async (t) => {
logger.log({level: 'INFO', topic: 'APP', message: 'Hello World!'})
return {status: 200}
}
`
`,
)

await sendRequest({
method: 'GET',
url: 'http://localhost:11006',
headers: {}
headers: {},
})
t.true(output[1].includes('[INFO] [HTTP] GET /'))
t.true(output[2].includes('[INFO] [APP] Hello World!'))
Expand Down
6 changes: 3 additions & 3 deletions http/connect/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ export default async function (url) {

pushedResponses.push([
{method, url, headers: requestHeaders},
{status, headers: responseHeaders, body}
{status, headers: responseHeaders, body},
])
})

return {
async sendRequest(request) {
const nodeResponse = client.request({
[HTTP2_HEADER_PATH]: request.url,
...request.headers
...request.headers,
})

const nodeHeaders = await pEvent(nodeResponse, 'response')
Expand All @@ -55,6 +55,6 @@ export default async function (url) {
await pEvent(client, 'close')
},

pushedResponses
pushedResponses,
}
}
Loading

0 comments on commit 33d4d5e

Please sign in to comment.