Skip to content

Commit 5085c8f

Browse files
committed
Misc
1 parent 624b1ea commit 5085c8f

File tree

6 files changed

+87
-102
lines changed

6 files changed

+87
-102
lines changed

eslint.config.mjs

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,9 @@ export default defineConfig(
4747
],
4848
'no-underscore-dangle': 'off',
4949
curly: 'error',
50+
eqeqeq: 'error',
5051
'@typescript-eslint/consistent-type-imports': 'error',
51-
'@typescript-eslint/no-explicit-any': 'warn',
52+
'@typescript-eslint/no-explicit-any': 'error',
5253
'@typescript-eslint/ban-ts-comment': [
5354
'error',
5455
{ 'ts-expect-error': 'allow-with-description', 'ts-ignore': true },
@@ -61,7 +62,6 @@ export default defineConfig(
6162
'unicorn/prefer-node-protocol': 'off',
6263
'unicorn/no-unreadable-array-destructuring': 'off',
6364
'unicorn/text-encoding-identifier-case': 'off',
64-
'unicorn/no-abusive-eslint-disable': 'off',
6565
'unicorn/no-array-callback-reference': 'off',
6666
'unicorn/number-literal-case': 'off',
6767
'unicorn/prefer-add-event-listener': 'off',
@@ -71,7 +71,6 @@ export default defineConfig(
7171
'unicorn/no-lonely-if': 'off',
7272
'unicorn/consistent-destructuring': 'off',
7373
'unicorn/prefer-module': 'off',
74-
'unicorn/prefer-optional-catch-binding': 'off',
7574
'unicorn/no-useless-undefined': 'off',
7675
'unicorn/no-null': 'off',
7776
'unicorn/no-nested-ternary': 'off',
@@ -80,7 +79,6 @@ export default defineConfig(
8079
'unicorn/prevent-abbreviations': 'off',
8180
'unicorn/prefer-code-point': 'off',
8281
'unicorn/numeric-separators-style': 'off',
83-
'unicorn/no-array-for-each': 'off',
8482
'unicorn/prefer-spread': 'off',
8583
'unicorn/explicit-length-check': 'off',
8684
'unicorn/prefer-regexp-test': 'off',
@@ -97,13 +95,7 @@ export default defineConfig(
9795
'unicorn/no-process-exit': 'off',
9896
'unicorn/prefer-at': 'off',
9997
'unicorn/prefer-blob-reading-methods': 'off',
100-
'@typescript-eslint/no-unsafe-member-access': 'off',
101-
'@typescript-eslint/no-unsafe-argument': 'off',
102-
'@typescript-eslint/no-unsafe-assignment': 'off',
103-
'@typescript-eslint/no-unsafe-call': 'off',
104-
'@typescript-eslint/no-unsafe-return': 'off',
105-
'@typescript-eslint/prefer-nullish-coalescing': 'off',
106-
'@typescript-eslint/require-await': 'off',
98+
'@typescript-eslint/prefer-nullish-coalescing': 'error',
10799
'@typescript-eslint/restrict-template-expressions': 'off',
108100
'@typescript-eslint/no-base-to-string': 'off',
109101
'@typescript-eslint/no-unnecessary-type-parameters': 'off',

src/blobFile.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,11 @@ export default class BlobFile implements GenericFilehandle {
6464
}
6565
}
6666

67-
public async stat(): Promise<Stats> {
68-
return { size: this.blob.size }
67+
public stat(): Promise<Stats> {
68+
return Promise.resolve({ size: this.blob.size })
6969
}
7070

71-
public async close(): Promise<void> {
72-
return
71+
public close(): Promise<void> {
72+
return Promise.resolve()
7373
}
7474
}

src/filehandle.ts

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,23 +18,10 @@ export type Fetcher = (
1818
init?: RequestInit,
1919
) => Promise<Response>
2020

21-
export type RequestOverrides = Omit<RequestInit, 'headers'> & {
22-
headers?: Record<string, string>
23-
}
24-
2521
export interface FilehandleOptions {
26-
/**
27-
* optional AbortSignal object for aborting the request
28-
*/
2922
signal?: AbortSignal
3023
headers?: Record<string, string>
31-
overrides?: RequestOverrides
3224
encoding?: BufferEncoding
33-
/**
34-
* fetch function to use for HTTP requests. defaults to environment's
35-
* global fetch. if there is no global fetch, and a fetch function is not provided,
36-
* throws an error.
37-
*/
3825
fetch?: Fetcher
3926
}
4027

src/remoteFile.ts

Lines changed: 20 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,16 @@ import type {
33
Fetcher,
44
FilehandleOptions,
55
GenericFilehandle,
6-
RequestOverrides,
76
Stats,
87
} from './filehandle.ts'
98

109
function getMessage(e: unknown) {
1110
const r =
12-
typeof e === 'object' && e !== null && 'message' in e
13-
? (e.message as string)
11+
typeof e === 'object' &&
12+
e !== null &&
13+
'message' in e &&
14+
typeof e.message === 'string'
15+
? e.message
1416
: `${e}`
1517
return r.replace(/\.$/, '')
1618
}
@@ -19,15 +21,15 @@ export default class RemoteFile implements GenericFilehandle {
1921
protected url: string
2022
private _stat?: Stats
2123
private fetchImplementation: Fetcher
22-
private baseOverrides: RequestOverrides = {}
24+
private baseHeaders: Record<string, string>
2325

2426
public constructor(source: string, opts: FilehandleOptions = {}) {
2527
this.url = source
26-
const fetch = opts.fetch || globalThis.fetch.bind(globalThis)
27-
if (opts.overrides) {
28-
this.baseOverrides = opts.overrides
29-
}
30-
this.fetchImplementation = fetch
28+
this.baseHeaders = opts.headers ?? {}
29+
this.fetchImplementation =
30+
opts.fetch ??
31+
((input: RequestInfo, init?: RequestInit) =>
32+
globalThis.fetch(input, init))
3133
}
3234

3335
public async fetch(
@@ -77,20 +79,14 @@ export default class RemoteFile implements GenericFilehandle {
7779
`read() called with NaN length or position (length=${length}, position=${position}). The index file may be corrupt.`,
7880
)
7981
}
80-
const { headers = {}, signal, overrides = {} } = opts
82+
const { headers = {}, signal } = opts
8183
if (length < Infinity) {
8284
headers.range = `bytes=${position}-${position + length - 1}`
8385
} else if (length === Infinity && position !== 0) {
8486
headers.range = `bytes=${position}-`
8587
}
8688
const res = await this.fetch(this.url, {
87-
...this.baseOverrides,
88-
...overrides,
89-
headers: {
90-
...this.baseOverrides.headers,
91-
...overrides.headers,
92-
...headers,
93-
},
89+
headers: { ...this.baseHeaders, ...headers },
9490
method: 'GET',
9591
redirect: 'follow',
9692
mode: 'cors',
@@ -104,7 +100,7 @@ export default class RemoteFile implements GenericFilehandle {
104100
if ((res.status === 200 && position === 0) || res.status === 206) {
105101
// try to parse out the size of the remote file
106102
const contentRange = res.headers.get('content-range')
107-
const sizeMatch = /\/(\d+)$/.exec(contentRange || '')
103+
const sizeMatch = /\/(\d+)$/.exec(contentRange ?? '')
108104
if (sizeMatch?.[1]) {
109105
this._stat = {
110106
size: parseInt(sizeMatch[1], 10),
@@ -145,25 +141,11 @@ export default class RemoteFile implements GenericFilehandle {
145141
public async readFile(
146142
options: FilehandleOptions | BufferEncoding = {},
147143
): Promise<Uint8Array<ArrayBuffer> | string> {
148-
let encoding
149-
let opts
150-
if (typeof options === 'string') {
151-
encoding = options
152-
opts = {}
153-
} else {
154-
encoding = options.encoding
155-
const { encoding: _, ...rest } = options
156-
opts = rest
157-
}
158-
const { headers = {}, signal, overrides = {} } = opts
144+
const encoding = typeof options === 'string' ? options : options.encoding
145+
const opts = typeof options === 'string' ? {} : options
146+
const { headers = {}, signal } = opts
159147
const res = await this.fetch(this.url, {
160-
...this.baseOverrides,
161-
...overrides,
162-
headers: {
163-
...this.baseOverrides.headers,
164-
...overrides.headers,
165-
...headers,
166-
},
148+
headers: { ...this.baseHeaders, ...headers },
167149
method: 'GET',
168150
redirect: 'follow',
169151
mode: 'cors',
@@ -193,7 +175,7 @@ export default class RemoteFile implements GenericFilehandle {
193175
return this._stat
194176
}
195177

196-
public async close(): Promise<void> {
197-
return
178+
public close(): Promise<void> {
179+
return Promise.resolve()
198180
}
199181
}

test/auth.test.ts

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,21 @@ function createResponse(
1919
status,
2020
headers: {
2121
get(name: string) {
22-
return headers[name] || null
22+
return headers[name] ?? null
2323
},
2424
},
25-
arrayBuffer: async () => {
25+
arrayBuffer: () => {
2626
if (typeof body === 'string') {
2727
const encoder = new TextEncoder()
28-
return encoder.encode(body).buffer
28+
return Promise.resolve(encoder.encode(body).buffer)
2929
}
30-
return body.buffer
30+
return Promise.resolve(body.buffer)
3131
},
32-
text: async () => {
32+
text: () => {
3333
if (typeof body === 'string') {
34-
return body
34+
return Promise.resolve(body)
3535
}
36-
return toString(body)
36+
return Promise.resolve(toString(body))
3737
},
3838
}
3939
}
@@ -43,7 +43,7 @@ let mockFetch: (input: RequestInfo, init?: RequestInit) => Promise<Response>
4343

4444
beforeEach(() => {
4545
// Reset the mock fetch implementation before each test
46-
mockFetch = vi.fn().mockImplementation(async (url: string) => {
46+
mockFetch = vi.fn().mockImplementation((url: string) => {
4747
throw new Error(`Unhandled fetch request to ${url}`)
4848
})
4949
})
@@ -56,7 +56,7 @@ test('auth token', async () => {
5656
mockFetch = vi
5757
.fn()
5858
.mockImplementation(
59-
async (_url: string, args: { headers: Record<string, string> }) => {
59+
(_url: string, args: { headers: Record<string, string> }) => {
6060
return args.headers.Authorization
6161
? createResponse('hello world', 200)
6262
: createResponse('Unauthorized', 403)
@@ -65,10 +65,8 @@ test('auth token', async () => {
6565

6666
const f = new RemoteFile('http://fakehost/test.txt', {
6767
fetch: mockFetch,
68-
overrides: {
69-
headers: {
70-
Authorization: 'Basic YWxhZGRpbjpvcGVuc2VzYW1l',
71-
},
68+
headers: {
69+
Authorization: 'Basic YWxhZGRpbjpvcGVuc2VzYW1l',
7270
},
7371
})
7472
const stat = await f.readFile('utf8')
@@ -79,7 +77,7 @@ test('auth token with range request', async () => {
7977
mockFetch = vi
8078
.fn()
8179
.mockImplementation(
82-
async (_url: string, args: { headers: Record<string, string> }) => {
80+
(_url: string, args: { headers: Record<string, string> }) => {
8381
if (args.headers.Authorization && args.headers.range) {
8482
return createResponse('hello', 206)
8583
} else if (!args.headers.Authorization) {
@@ -93,10 +91,8 @@ test('auth token with range request', async () => {
9391

9492
const f = new RemoteFile('http://fakehost/test.txt', {
9593
fetch: mockFetch,
96-
overrides: {
97-
headers: {
98-
Authorization: 'Basic YWxhZGRpbjpvcGVuc2VzYW1l',
99-
},
94+
headers: {
95+
Authorization: 'Basic YWxhZGRpbjpvcGVuc2VzYW1l',
10096
},
10197
})
10298
const buffer = await f.read(5, 0)

0 commit comments

Comments
 (0)