Skip to content

Commit

Permalink
chore: yarn/lint
Browse files Browse the repository at this point in the history
  • Loading branch information
awlayton committed Jun 21, 2024
1 parent dff86e7 commit 6ff7f42
Show file tree
Hide file tree
Showing 17 changed files with 1,627 additions and 1,516 deletions.
28 changes: 15 additions & 13 deletions .eslintrc.yaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
root: true

extends:
- 'eslint:recommended'
- 'plugin:@typescript-eslint/recommended'
- eslint:recommended
- plugin:@typescript-eslint/recommended
- plugin:node/recommended
- plugin:github/recommended
- plugin:promise/recommended
Expand All @@ -12,10 +12,10 @@ extends:
- plugin:import/recommended
- plugin:unicorn/recommended
#- plugin:security/recommended
- plugin:sonarjs/recommended
#- plugin:sonarjs/recommended
- plugin:ava/recommended
- xo
- prettier
- plugin:prettier/recommended

plugins:
- '@typescript-eslint'
Expand All @@ -30,27 +30,28 @@ plugins:
- unicorn
- security
- no-secrets
- sonarjs
#- sonarjs
- ava
- notice

parser: '@typescript-eslint/parser'

parserOptions:
ecmaVersion: 2020
project: './tsconfig.*'
project: ./tsconfig.*

overrides:
- files: '*.{c,m,}ts'
extends:
- plugin:github/typescript
- plugin:import/typescript
- xo-typescript
- prettier
- plugin:prettier/recommended
parserOptions:
ecmaVersion: 2020
project: './tsconfig.*'
project: ./tsconfig.*
rules:
prettier/prettier: off
'@typescript-eslint/naming-convention':
[
error,
Expand Down Expand Up @@ -91,14 +92,15 @@ overrides:
{
args: after-used,
ignoreRestSiblings: true,
varsIgnorePattern: '^_',
argsIgnorePattern: '^_',
varsIgnorePattern: ^_,
argsIgnorePattern: ^_,
},
]
'@typescript-eslint/consistent-type-definitions': [warn, interface]
'@typescript-eslint/switch-exhaustiveness-check': off

rules:
prettier/prettier: off
notice/notice:
- error
- template: |
Expand All @@ -120,9 +122,9 @@ rules:
*/
onNonMatchingHeader: append
nonMatchingTolerance: 0.7
#'@typescript-eslint/no-shadow': [warn, { allow: [_] }]
sonarjs/no-duplicate-string: [warn, { threshold: 5 }]
sonarjs/cognitive-complexity: warn
'@typescript-eslint/no-shadow': [warn, { allow: [_] }]
#sonarjs/no-duplicate-string: [warn, { threshold: 5 }]
#sonarjs/cognitive-complexity: warn
eslint-comments/no-unused-disable: off
node/no-unpublished-import: off
spaced-comment: [error, always, markers: [/, //]]
Expand Down
6 changes: 5 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"typescript.enablePromptUseWorkspaceTsdk": true,
"cSpell.words": [
"esbuild",
"escompat",
"ksuid",
"lcov",
"sonarjs",
Expand Down Expand Up @@ -62,5 +63,8 @@
"testify.testRunnerPath": "yarn test",
"testify.envVars": {
"NODE_ENV": "test"
}
},
"eslint.rules.customizations": [
{ "rule": "prettier/prettier", "severity": "off" }
]
}
893 changes: 0 additions & 893 deletions .yarn/releases/yarn-4.1.1.cjs

This file was deleted.

894 changes: 894 additions & 0 deletions .yarn/releases/yarn-4.3.1.cjs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion .yarnrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ compressionLevel: mixed

enableGlobalCache: false

yarnPath: .yarn/releases/yarn-4.1.1.cjs
yarnPath: .yarn/releases/yarn-4.3.1.cjs
9 changes: 6 additions & 3 deletions lib/auto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@
import debug from 'debug';
import resolveALPN from 'resolve-alpn';

import { HttpClient } from './http.js';
import { type HTTPTimeouts, HttpClient } from './http.js';
import { WebSocketClient } from './websocket.js';
import { HTTPTimeouts } from './http.js';

const error = debug('@oada/client:auto:error');

Expand Down Expand Up @@ -92,7 +91,11 @@ export async function autoConnection({
switch (alpnProtocol) {
// Prefer HTTP/2
case 'h2': {
return new HttpClient(domain, token, { concurrency, userAgent, timeouts });
return new HttpClient(domain, token, {
concurrency,
userAgent,
timeouts,
});
}

// If no HTTP/2, use a WebSocket
Expand Down
16 changes: 8 additions & 8 deletions lib/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { generate as ksuid } from 'xksuid';

import type { Tree, TreeKey } from '@oada/types/oada/tree/v1.js';

import { type HTTPTimeouts, HttpClient } from './http.js';
import {
createNestedObject,
fixError,
Expand All @@ -35,7 +36,6 @@ import {
toStringPath,
} from './utils.js';
import { AbortController } from '#fetch';
import { HttpClient, type HTTPTimeouts } from './http.js';
import { WebSocketClient } from './websocket.js';

import type { Change, Json, JsonObject } from './index.js';
Expand Down Expand Up @@ -66,9 +66,9 @@ export type ConnectionRequest = {
headers: Record<string, string>;
data?: Body;
} & (
| { watch?: false }
| { watch: true; method: 'head' | 'get' | 'put' | 'post' | 'delete' }
);
| { watch?: false }
| { watch: true; method: 'head' | 'get' | 'put' | 'post' | 'delete' }
);

export interface ConnectionResponse {
requestId: string | string[];
Expand Down Expand Up @@ -106,7 +106,7 @@ export interface Config {
/** @default 'auto' */
connection?: 'auto' | 'ws' | 'http' | Connection;
userAgent?: string;
timeouts?: number | Partial<HTTPTimeouts>
timeouts?: number | Partial<HTTPTimeouts>;
}

export type Response = ConnectionResponse;
Expand Down Expand Up @@ -437,8 +437,8 @@ export class OADAClient {
});
const rev =
typeof data === 'object' &&
!(data instanceof Uint8Array) &&
!Array.isArray(data)
!(data instanceof Uint8Array) &&
!Array.isArray(data)
? Number(data?._rev)
: undefined;

Expand Down Expand Up @@ -792,7 +792,7 @@ export class OADAClient {
}

trace('Path to ensure did not exist. Creating');
return await this.put(request);
return this.put(request);
}
}

Expand Down
11 changes: 6 additions & 5 deletions lib/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import { setTimeout } from 'isomorphic-timers-promises';
import { Headers } from '#fetch';
import debug from 'debug';

import type { HeadersInit } from 'undici';
import { fixError } from './utils.js';

const warn = debug('@oada/client:errors:warn');
Expand All @@ -51,7 +52,7 @@ async function handleRatelimit<R extends unknown[], P>(
...rest: R
) {
// @ts-expect-error stupid errors
const headers = new Headers(error.headers);
const headers = new Headers(error.headers as HeadersInit);

// Figure out how many ms to wait
// Header is either number of seconds, or a date/time
Expand Down Expand Up @@ -104,22 +105,22 @@ export async function handleErrors<R extends unknown[], P>(
// @ts-expect-error stupid error handling
switch (`${error.status ?? cError?.code}`) {
case '429': {
return await handleRatelimit(error, request, ...rest);
return handleRatelimit(error, request, ...rest);
}

// Some servers use 503 for rate limit...
case '503': {
const headers = new Headers(error.headers);
const headers = new Headers(error.headers as HeadersInit);
if (headers.has('Retry-After')) {
return await handleRatelimit(error, request, ...rest);
return handleRatelimit(error, request, ...rest);
}

// If no Retry-After, don't assume rate-limit?
break;
}

case 'ECONNRESET': {
return await handleReset(error, request, ...rest);
return handleReset(error, request, ...rest);
}

default:
Expand Down
1 change: 1 addition & 0 deletions lib/event-iterator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@
* limitations under the License.
*/

// @ts-expect-error type bs
export { on, once } from 'node:events';
9 changes: 6 additions & 3 deletions lib/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ const enum ConnectionStatus {

export interface HTTPTimeouts {
/** @default 60e3 */
connect?: number
connect?: number;
body?: number;
headers?: number;
keepAlive?: number;
Expand Down Expand Up @@ -83,7 +83,11 @@ export class HttpClient extends EventEmitter implements Connection {
constructor(
domain: string,
token: string,
{ concurrency = 10, userAgent, timeouts }: { concurrency: number; userAgent: string; timeouts: HTTPTimeouts },
{
concurrency = 10,
userAgent,
timeouts,
}: { concurrency: number; userAgent: string; timeouts: HTTPTimeouts },
) {
super();

Expand Down Expand Up @@ -293,7 +297,6 @@ export class HttpClient extends EventEmitter implements Connection {
}

// @ts-expect-error stupid error handling
// eslint-disable-next-line sonarjs/no-small-switch
switch (cError?.code) {
// Happens when the HTTP/2 session is killed
case 'ERR_HTTP2_INVALID_SESSION': {
Expand Down
27 changes: 15 additions & 12 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,15 @@ export async function connect({
timeouts: t = {},
...config
}: Config & { token: string }): Promise<OADAClient> {
const timeouts = typeof t === 'number' ? {
connect: t,
keepAlive: t,
headers: t,
body: t,
} : t;
const timeouts =
typeof t === 'number'
? {
connect: t,
keepAlive: t,
headers: t,
body: t,
}
: t;
const connection =
proto === 'auto'
? await autoConnection({ concurrency, userAgent, timeouts, ...config })
Expand Down Expand Up @@ -85,12 +88,12 @@ export type Json = JsonPrimitive | JsonObject | JsonArray;

export type JsonCompatible<T> = {
[P in keyof T]: T[P] extends Json
? T[P]
: Pick<T, P> extends Required<Pick<T, P>>
? never
: T[P] extends (() => unknown) | undefined
? never
: JsonCompatible<T[P]>;
? T[P]
: Pick<T, P> extends Required<Pick<T, P>>
? never
: T[P] extends (() => unknown) | undefined
? never
: JsonCompatible<T[P]>;
};

declare global {
Expand Down
39 changes: 25 additions & 14 deletions lib/websocket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,19 +61,30 @@ interface ResponseEmitter extends EventEmitter {
): this;
}

declare module 'events' {
// eslint-disable-next-line unicorn/no-static-only-class, @typescript-eslint/no-extraneous-class, @typescript-eslint/no-shadow
class EventEmitter {
static once(
emitter: ResponseEmitter,
event: `response:${string}`,
): Promise<[ConnectionResponse]>;
static on(
emitter: ResponseEmitter,
event: `change:${string}`,
options?: { signal?: AbortSignal },
): AsyncIterableIterator<[ConnectionChange]>;
}
declare module '#event-iterator' {
// @ts-expect-error type bs
// eslint-disable-next-line @typescript-eslint/no-shadow
export function once(
emitter: ResponseEmitter,
event: `response:${string}`,
): Promise<[ConnectionResponse]>;
// @ts-expect-error type bs
export function once(
emitter: _ReconnectingWebSocket.default,
event: 'error',
): Promise<[Error]>;
// @ts-expect-error type bs
export function once(
emitter: _ReconnectingWebSocket.default,
event: 'open',
): Promise<void>;
// @ts-expect-error type bs
// eslint-disable-next-line @typescript-eslint/no-shadow
export function on(
emitter: ResponseEmitter,
event: `change:${string}`,
options?: { signal?: AbortSignal },
): AsyncIterableIterator<[ConnectionChange]>;
}

const enum ConnectionStatus {
Expand Down Expand Up @@ -128,7 +139,7 @@ export class WebSocketClient extends EventEmitter implements Connection {
const openP = once(ws, 'open').then(() => ws);
// eslint-disable-next-line github/no-then
const errorP = once(ws, 'error').then(([wsError]) => {
throw wsError as Error;
throw wsError;
});
this.#ws = Promise.race([openP, errorP]);

Expand Down
Loading

0 comments on commit 6ff7f42

Please sign in to comment.