Skip to content

Commit

Permalink
Misc
Browse files Browse the repository at this point in the history
  • Loading branch information
cmdcolin committed Jul 22, 2024
1 parent f4a854c commit de549e3
Show file tree
Hide file tree
Showing 14 changed files with 1,949 additions and 2,024 deletions.
18 changes: 0 additions & 18 deletions .eslintrc.json

This file was deleted.

19 changes: 9 additions & 10 deletions .github/workflows/push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,18 @@ name: Node.js CI

on:
push:
branches: [ master ]
branches: [master]
pull_request:
branches: [ master ]
branches: [master]

jobs:
build:

runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v2
with:
node-version: 14.x
- run: yarn install --frozen-lockfile
- run: yarn test
- uses: actions/checkout@v4
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v4
with:
node-version: 20.x
- run: yarn install --frozen-lockfile
- run: yarn test
56 changes: 56 additions & 0 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import prettier from 'eslint-plugin-prettier'
import typescriptEslint from '@typescript-eslint/eslint-plugin'
import tsParser from '@typescript-eslint/parser'
import path from 'node:path'
import { fileURLToPath } from 'node:url'
import js from '@eslint/js'
import { FlatCompat } from '@eslint/eslintrc'

const __filename = fileURLToPath(import.meta.url)
const __dirname = path.dirname(__filename)
const compat = new FlatCompat({
baseDirectory: __dirname,
recommendedConfig: js.configs.recommended,
allConfig: js.configs.all,
})

export default [
...compat.extends(
'plugin:@typescript-eslint/recommended',
'plugin:@typescript-eslint/recommended-type-checked',
'plugin:@typescript-eslint/stylistic-type-checked',
'plugin:prettier/recommended',
'plugin:unicorn/recommended',
),
{
plugins: {
prettier,
'@typescript-eslint': typescriptEslint,
},

languageOptions: {
parser: tsParser,
ecmaVersion: 5,
sourceType: 'script',

parserOptions: {
project: './tsconfig.lint.json',
},
},

rules: {
'no-underscore-dangle': 0,
curly: 'error',
'unicorn/no-null': 0,
'unicorn/filename-case': 0,
'@typescript-eslint/filename-case': 0,
'@typescript-eslint/no-explicit-any': 0,
'@typescript-eslint/explicit-module-boundary-types': 0,
'@typescript-eslint/ban-ts-comment': 0,
'@typescript-eslint/ban-types': 0,
'@typescript-eslint/require-await': 0,
'@typescript-eslint/no-empty-function': 0,
semi: ['error', 'never'],
},
},
]
34 changes: 16 additions & 18 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,37 +23,35 @@
"test": "jest",
"coverage": "npm test -- --coverage",
"postcoverage": "opn coverage/lcov-report/index.html",
"lint": "eslint src test",
"lint": "eslint --report-unused-disable-directives --max-warnings 0 src test",
"docs": "documentation readme --shallow src/AbortablePromiseCache.js --section=API",
"clean": "rimraf dist esm",
"prebuild": "npm run clean",
"build:esm": "tsc --outDir esm",
"build:es5": "tsc --module commonjs --outDir dist",
"build:esm": "tsc --target es2018 --outDir esm",
"build:es5": "tsc --target es2015 --module commonjs --outDir dist",
"build": "npm run build:esm && npm run build:es5",
"prepublishOnly": "npm run lint && npm test && npm run build",
"postversion": "git push --follow-tags"
},
"keywords": [],
"dependencies": {
"abortcontroller-polyfill": "^1.2.9"
},
"devDependencies": {
"@eslint/eslintrc": "^3.1.0",
"@eslint/js": "^9.7.0",
"@types/jest": "^29.2.4",
"@types/node": "^18.11.16",
"@typescript-eslint/eslint-plugin": "^5.46.1",
"@typescript-eslint/parser": "^5.46.1",
"@types/node": "^20.14.11",
"@typescript-eslint/eslint-plugin": "^7.17.0",
"@typescript-eslint/parser": "^7.17.0",
"documentation": "^14.0.1",
"eslint": "^8.30.0",
"eslint-config-airbnb-base": "^15.0.0",
"eslint-config-prettier": "^8.5.0",
"eslint-plugin-import": "^2.26.0",
"eslint-plugin-prettier": "^4.2.1",
"eslint": "^9.7.0",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-prettier": "^5.2.1",
"eslint-plugin-unicorn": "^54.0.0",
"jest": "^29.3.1",
"prettier": "^2.8.1",
"quick-lru": "^2.0.0",
"rimraf": "^3.0.2",
"prettier": "^3.3.3",
"quick-lru": "^4.0.0",
"rimraf": "^6.0.1",
"ts-jest": "^29.0.3",
"typescript": "^4.9.4"
"typescript": "^5.5.4"
},
"publishConfig": {
"access": "public"
Expand Down
17 changes: 8 additions & 9 deletions src/AbortablePromiseCache.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { AbortSignal } from './abortcontroller-ponyfill'
import AggregateAbortController from './AggregateAbortController'
import AggregateStatusReporter from './AggregateStatusReporter'

type Cache<U> = {
interface Cache<U> {
delete: (key: string) => void
keys: () => Iterator<string>
get: (key: string) => U | undefined
set: (key: string, val: U) => void
set: (key: string, value: U) => void
has: (key: string) => boolean
}
type FillCallback<T, U> = (
Expand All @@ -15,7 +14,7 @@ type FillCallback<T, U> = (
statusCallback?: Function,
) => Promise<U>

type Entry<U> = {
interface Entry<U> {
aborter: AggregateAbortController
settled: boolean
readonly aborted: boolean
Expand Down Expand Up @@ -117,11 +116,11 @@ export default class AbortablePromiseCache<T, U> {
this.evict(key, newEntry)
},
)
.catch(e => {
.catch(error => {
// this will only be reached if there is some kind of
// bad bug in this library
console.error(e)
throw e
console.error(error)
throw error
})

this.cache.set(key, newEntry)
Expand All @@ -132,7 +131,7 @@ export default class AbortablePromiseCache<T, U> {
// promise if it was, regardless of what happened with the cached
// response
function checkForSingleAbort() {
if (signal && signal.aborted) {
if (signal?.aborted) {
throw Object.assign(new Error('aborted'), { code: 'ERR_ABORTED' })
}
}
Expand Down Expand Up @@ -206,7 +205,7 @@ export default class AbortablePromiseCache<T, U> {
this.fill(key, data, signal, statusCallback)
return AbortablePromiseCache.checkSinglePromise(
//see https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#non-null-assertion-operator-postfix-
//eslint-disable-next-line @typescript-eslint/no-non-null-assertion

this.cache.get(key)!.promise,
signal,
)
Expand Down
2 changes: 0 additions & 2 deletions src/AggregateAbortController.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { AbortController, AbortSignal } from './abortcontroller-ponyfill'

class NullSignal {}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/AggregateStatusReporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ export default class AggregateStatusReporter {

callback(message: unknown) {
this.currentMessage = message
this.callbacks.forEach(elt => {
for (const elt of this.callbacks) {
elt(message)
})
}
}
}
23 changes: 0 additions & 23 deletions src/abortcontroller-ponyfill.ts

This file was deleted.

1 change: 0 additions & 1 deletion src/declare.d.ts

This file was deleted.

4 changes: 1 addition & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
import AbortablePromiseCache from './AbortablePromiseCache'

export default AbortablePromiseCache
export { default } from './AbortablePromiseCache'
30 changes: 14 additions & 16 deletions test/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
//@ts-nocheck
import { AbortController } from '../src/abortcontroller-ponyfill'
import QuickLRU from 'quick-lru'

import AbortablePromiseCache from '../src'

jest.useFakeTimers()

//eslint-disable-next-line @typescript-eslint/no-var-requires
const QuickLRU = require('quick-lru')

function delay(ms) {
function delay(ms: number) {
return new Promise(r => setTimeout(r, ms))
}

Expand Down Expand Up @@ -49,6 +46,7 @@ test('arg check', async () => {

const aborter = new AbortController()
expect(() => {
// eslint-disable-next-line @typescript-eslint/no-floating-promises
cache.get('foo', aborter.signal)
aborter.abort()
}).toThrow(/perhaps you meant/)
Expand Down Expand Up @@ -80,7 +78,7 @@ test('cache 2 requests, one aborted', async () => {
let fillAborted = false
const cache = new AbortablePromiseCache({
cache: new QuickLRU({ maxSize: 2 }),
async fill({ whichCall }, signal) {
async fill({ whichCall }: { whichCall: number }, signal) {
callCount += 1
which = whichCall
await delay(30)
Expand Down Expand Up @@ -116,7 +114,7 @@ test('cache 2 requests, both aborted, and fill aborted', async () => {
let fillAborted = false
const cache = new AbortablePromiseCache({
cache: new QuickLRU({ maxSize: 2 }),
async fill({ whichCall }, signal) {
async fill({ whichCall }: { whichCall: number }, signal) {
callCount += 1
which = whichCall
await delay(30)
Expand Down Expand Up @@ -151,7 +149,7 @@ test('cache 2 requests, both aborted, one pre-aborted, and fill aborted', async
let fillAborted = false
const cache = new AbortablePromiseCache({
cache: new QuickLRU({ maxSize: 2 }),
async fill({ whichCall }, signal) {
async fill({ whichCall }: { whichCall: number }, signal) {
callCount += 1
which = whichCall
await delay(30)
Expand Down Expand Up @@ -187,7 +185,7 @@ test('cache 2 requests, abort one and wait for it, then make another and check t
let abortCount = 0
const cache = new AbortablePromiseCache({
cache: new QuickLRU({ maxSize: 2 }),
async fill({ whichCall }, signal) {
async fill({ whichCall }: { whichCall: number }, signal) {
callCount += 1
which = whichCall
await delay(30)
Expand Down Expand Up @@ -222,7 +220,7 @@ test('cache 3 requests, 2 aborted, but fill and last request did not abort', asy
let fillAborted = false
const cache = new AbortablePromiseCache({
cache: new QuickLRU({ maxSize: 2 }),
async fill({ whichCall }, signal) {
async fill({ whichCall }: { whichCall: number }, signal) {
callCount += 1
which = whichCall
await delay(30)
Expand Down Expand Up @@ -259,7 +257,7 @@ test('deleting aborts', async () => {
let abortCount = 0
const cache = new AbortablePromiseCache({
cache: new QuickLRU({ maxSize: 2 }),
async fill({ whichCall }, signal) {
async fill({ whichCall }: { whichCall: number }, signal) {
callCount += 1
which = whichCall
await delay(30)
Expand Down Expand Up @@ -289,7 +287,7 @@ test('clear can delete zero', async () => {
let abortCount = 0
const cache = new AbortablePromiseCache({
cache: new QuickLRU({ maxSize: 2 }),
async fill({ whichCall }, signal) {
async fill({ whichCall }: { whichCall: number }, signal) {
callCount += 1
which = whichCall
await delay(30)
Expand All @@ -313,7 +311,7 @@ test('clear can delete one', async () => {
let abortCount = 0
const cache = new AbortablePromiseCache({
cache: new QuickLRU({ maxSize: 2 }),
async fill({ whichCall }, signal) {
async fill({ whichCall }: { whichCall: number }, signal) {
callCount += 1
which = whichCall
await delay(30)
Expand Down Expand Up @@ -343,7 +341,7 @@ test('clear can delete two', async () => {
let abortCount = 0
const cache = new AbortablePromiseCache({
cache: new QuickLRU({ maxSize: 2 }),
async fill({ whichCall }, signal) {
async fill({ whichCall }: { whichCall: number }, signal) {
callCount += 1
which = whichCall
await delay(30)
Expand Down Expand Up @@ -377,11 +375,11 @@ test('clear can delete two', async () => {
})

test('not caching errors', async () => {
let i = 0
let index = 0
const cache = new AbortablePromiseCache({
cache: new QuickLRU({ maxSize: 2 }),
async fill() {
if (i++ === 0) {
if (index++ === 0) {
throw new Error('first time')
} else {
return 42
Expand Down
5 changes: 2 additions & 3 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
{
"compilerOptions": {
"lib": ["es2017", "es7", "es6", "dom"],
"target": "es2018",
"moduleResolution": "node",
"target": "es2015",
"declaration": true,
"outDir": "dist",
"moduleResolution": "node",
"strict": true,
"sourceMap": true,
"esModuleInterop": true
Expand Down
Loading

0 comments on commit de549e3

Please sign in to comment.