-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22 from truework/beta
feat: new build pipeline, tests, npm audit
- Loading branch information
Showing
14 changed files
with
26,529 additions
and
15,865 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
#!/bin/sh | ||
. "$(dirname "$0")/_/husky.sh" | ||
|
||
npx --no-install prettier-standard --format --staged && npm run test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
#!/bin/sh | ||
. "$(dirname "$0")/_/husky.sh" | ||
|
||
exec < /dev/tty && node_modules/.bin/cz --hook || true |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,236 @@ | ||
import 'cross-fetch/polyfill' | ||
import { createServer } from 'http' | ||
|
||
import { handleRetry } from '../lib/handleRetry' | ||
import { handleTimeout } from '../lib/handleTimeout' | ||
|
||
export default (test, assert) => { | ||
test('works', async () => { | ||
let i = 0 | ||
const server = createServer((req, res) => { | ||
if (i++ < 2) { | ||
res.writeHead(500) | ||
res.end() | ||
} else { | ||
res.end('ha') | ||
} | ||
}) | ||
|
||
await new Promise(r => { | ||
server.listen(async () => { | ||
// @ts-ignore | ||
const { port } = server.address() | ||
|
||
const raw = await handleRetry( | ||
() => fetch(`http://127.0.0.1:${port}`), | ||
'GET', | ||
{} | ||
) | ||
const res = await raw.text() | ||
|
||
assert.equal(res, 'ha') | ||
|
||
server.close() | ||
|
||
r(0) | ||
}) | ||
}) | ||
}) | ||
|
||
test('retries fail', async () => { | ||
let i = 0 | ||
const server = createServer((req, res) => { | ||
if (i++ < 2) { | ||
res.writeHead(500) | ||
res.end() | ||
} else { | ||
res.end('ha') | ||
} | ||
}) | ||
|
||
await new Promise(r => { | ||
server.listen(async () => { | ||
// @ts-ignore | ||
const { port } = server.address() | ||
|
||
const raw = await handleRetry( | ||
() => fetch(`http://127.0.0.1:${port}`), | ||
'GET', | ||
{ attempts: 1 } | ||
) | ||
assert.equal(raw.status, 500) | ||
|
||
server.close() | ||
|
||
r(0) | ||
}) | ||
}) | ||
}) | ||
|
||
test('respect 0 retries config', async () => { | ||
let i = 0 | ||
const server = createServer((req, res) => { | ||
if (i++ < 2) { | ||
res.writeHead(500) | ||
res.end() | ||
} else { | ||
res.end('ha') | ||
} | ||
}) | ||
|
||
await new Promise(r => { | ||
server.listen(async () => { | ||
// @ts-ignore | ||
const { port } = server.address() | ||
|
||
const raw = await handleRetry( | ||
() => fetch(`http://127.0.0.1:${port}`), | ||
'GET', | ||
{ attempts: 0 } | ||
) | ||
assert.equal(raw.status, 500) | ||
assert.equal(i, 1) | ||
|
||
server.close() | ||
|
||
r(0) | ||
}) | ||
}) | ||
}) | ||
|
||
test('retries for specified status codes', async () => { | ||
let i = 0 | ||
const server = createServer((req, res) => { | ||
if (i++ < 2) { | ||
res.writeHead(400) | ||
res.end() | ||
} else { | ||
res.end('ha') | ||
} | ||
}) | ||
|
||
await new Promise(r => { | ||
server.listen(async () => { | ||
// @ts-ignore | ||
const { port } = server.address() | ||
|
||
const raw = await handleRetry( | ||
() => fetch(`http://127.0.0.1:${port}`), | ||
'GET', | ||
{ codes: [400] } | ||
) | ||
const res = await raw.text() | ||
|
||
assert.equal(res, 'ha') | ||
|
||
server.close() | ||
|
||
r(0) | ||
}) | ||
}) | ||
}) | ||
|
||
test('retries for specified methods', async () => { | ||
let i = 0 | ||
const server = createServer((req, res) => { | ||
if (i++ < 2) { | ||
res.writeHead(500) | ||
res.end() | ||
} else { | ||
res.end('ha') | ||
} | ||
}) | ||
|
||
await new Promise(r => { | ||
server.listen(async () => { | ||
// @ts-ignore | ||
const { port } = server.address() | ||
|
||
const raw = await handleRetry( | ||
() => fetch(`http://127.0.0.1:${port}`), | ||
'POST', | ||
{ methods: ['POST'] } | ||
) | ||
const res = await raw.text() | ||
|
||
assert.equal(res, 'ha') | ||
|
||
server.close() | ||
|
||
r(0) | ||
}) | ||
}) | ||
}) | ||
|
||
test('works with timeout', async () => { | ||
const server = createServer((req, res) => { | ||
setTimeout(() => { | ||
res.end('ha') | ||
}, 1000) | ||
}) | ||
|
||
await new Promise(r => { | ||
server.listen(async () => { | ||
// @ts-ignore | ||
const { port } = server.address() | ||
|
||
const request = () => | ||
handleTimeout(fetch(`http://127.0.0.1:${port}`), 500) | ||
|
||
try { | ||
await handleRetry(request, 'GET', {}) | ||
} catch (e) { | ||
assert.equal(e.name, 'HTTPTimeout') | ||
} | ||
|
||
server.close() | ||
|
||
r(0) | ||
}) | ||
}) | ||
}) | ||
|
||
test('respects Retry-After header', async () => { | ||
let i = 0 | ||
const server = createServer((req, res) => { | ||
if (i++ < 2) { | ||
res.writeHead(500, { | ||
'Retry-After': 1 | ||
}) | ||
res.end() | ||
} else { | ||
res.end('ha') | ||
} | ||
}) | ||
|
||
await new Promise(r => { | ||
server.listen(async () => { | ||
// @ts-ignore | ||
const { port } = server.address() | ||
|
||
const then = Date.now() | ||
|
||
const raw = await handleRetry( | ||
() => fetch(`http://127.0.0.1:${port}`), | ||
'GET', | ||
{} | ||
) | ||
|
||
const now = Date.now() | ||
|
||
// retried too fast | ||
if (now - then < 1000) { | ||
throw Error('fail') | ||
} | ||
|
||
const res = await raw.text() | ||
|
||
assert.equal(res, 'ha') | ||
|
||
server.close() | ||
|
||
r(0) | ||
}) | ||
}) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import 'cross-fetch/polyfill' | ||
import { createServer } from 'http' | ||
|
||
import { handleTimeout } from '../lib/handleTimeout' | ||
|
||
export default (test, assert) => { | ||
test('will timeout', async () => { | ||
const server = createServer((req, res) => { | ||
setTimeout(() => { | ||
res.end('ha') | ||
}, 1000) | ||
}) | ||
|
||
await new Promise(r => { | ||
server.listen(async () => { | ||
// @ts-ignore | ||
const { port } = server.address() | ||
|
||
try { | ||
await handleTimeout(fetch(`http://127.0.0.1:${port}`), 500) | ||
} catch (e) { | ||
assert.equal(e.name, 'HTTPTimeout') | ||
} | ||
|
||
server.close() | ||
|
||
r(0) | ||
}) | ||
}) | ||
}) | ||
|
||
test("won't timeout", async () => { | ||
const server = createServer((req, res) => { | ||
setTimeout(() => { | ||
res.end('ha') | ||
}, 1000) | ||
}) | ||
|
||
await new Promise(r => { | ||
server.listen(async () => { | ||
// @ts-ignore | ||
const { port } = server.address() | ||
|
||
const raw = await handleTimeout(fetch(`http://127.0.0.1:${port}`)) | ||
const res = await raw.text() | ||
|
||
assert.equal(res, 'ha') | ||
|
||
server.close() | ||
|
||
r(0) | ||
}) | ||
}) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
console.time('test') | ||
|
||
require('esbuild-register/dist/node').register() | ||
|
||
const test = require('baretest')('gretchen') | ||
const assert = require('assert') | ||
|
||
require('./utils.test').default(test, assert) | ||
require('./handleRetry.test').default(test, assert) | ||
require('./handleTimeout.test').default(test, assert) | ||
require('./index.test').default(test, assert) | ||
|
||
process.on('unhandledRejection', e => { | ||
console.error(e) | ||
process.exit(1) | ||
}) | ||
|
||
!(async function () { | ||
test.before(() => {}) | ||
|
||
test.after(() => {}) | ||
|
||
await test.run() | ||
|
||
console.timeEnd('test') | ||
})() |
Oops, something went wrong.