Skip to content

Commit 31e94a6

Browse files
committed
fix: export TextEncoder from text-encoder
1 parent bed03d0 commit 31e94a6

File tree

3 files changed

+44
-24
lines changed

3 files changed

+44
-24
lines changed

src/http.js

Lines changed: 42 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
const fetch = require('node-fetch')
55
const merge = require('merge-options').bind({ ignoreUndefined: true })
66
const { URL, URLSearchParams } = require('iso-url')
7-
const TextDecoder = require('./text-encoder')
7+
const TextDecoder = require('./text-decoder')
88
const AbortController = require('abort-controller')
99
const anySignal = require('any-signal')
1010

@@ -47,8 +47,8 @@ const timeout = (promise, ms, abortController) => {
4747
}
4848
}, ms)
4949

50-
const after = (next) => {
51-
return (res) => {
50+
const after = next => {
51+
return res => {
5252
clearTimeout(timeoutID)
5353

5454
if (timedOut()) {
@@ -60,8 +60,7 @@ const timeout = (promise, ms, abortController) => {
6060
}
6161
}
6262

63-
promise
64-
.then(after(resolve), after(reject))
63+
promise.then(after(resolve), after(reject))
6564
})
6665
}
6766

@@ -112,14 +111,23 @@ class HTTP {
112111
opts.headers = new Headers(opts.headers)
113112

114113
// validate resource type
115-
if (typeof resource !== 'string' && !(resource instanceof URL || resource instanceof Request)) {
114+
if (
115+
typeof resource !== 'string' &&
116+
!(resource instanceof URL || resource instanceof Request)
117+
) {
116118
throw new TypeError('`resource` must be a string, URL, or Request')
117119
}
118120

119121
// validate resource format and normalize with prefixUrl
120-
if (opts.base && typeof opts.base === 'string' && typeof resource === 'string') {
122+
if (
123+
opts.base &&
124+
typeof opts.base === 'string' &&
125+
typeof resource === 'string'
126+
) {
121127
if (resource.startsWith('/')) {
122-
throw new Error('`resource` must not begin with a slash when using `base`')
128+
throw new Error(
129+
'`resource` must not begin with a slash when using `base`'
130+
)
123131
}
124132

125133
if (!opts.base.endsWith('/')) {
@@ -133,7 +141,9 @@ class HTTP {
133141
const url = new URL(resource, opts.base)
134142

135143
if (opts.searchParams) {
136-
url.search = opts.transformSearchParams(new URLSearchParams(opts.searchParams))
144+
url.search = opts.transformSearchParams(
145+
new URLSearchParams(opts.searchParams)
146+
)
137147
}
138148

139149
if (opts.json !== undefined) {
@@ -144,11 +154,15 @@ class HTTP {
144154
const abortController = new AbortController()
145155
const signal = anySignal([abortController.signal, opts.signal])
146156

147-
const response = await timeout(fetch(url, {
148-
...opts,
149-
signal,
150-
timeout: undefined
151-
}), opts.timeout, abortController)
157+
const response = await timeout(
158+
fetch(url, {
159+
...opts,
160+
signal,
161+
timeout: undefined
162+
}),
163+
opts.timeout,
164+
abortController
165+
)
152166

153167
if (!response.ok && opts.throwHttpErrors) {
154168
if (opts.handleError) {
@@ -161,7 +175,7 @@ class HTTP {
161175
const it = streamToAsyncIterator(response.body)
162176

163177
if (!isAsyncIterator(it)) {
164-
throw new Error('Can\'t convert fetch body into a Async Iterator:')
178+
throw new Error("Can't convert fetch body into a Async Iterator:")
165179
}
166180

167181
return it
@@ -273,7 +287,10 @@ const ndjson = async function * (source) {
273287
const streamToAsyncIterator = function (source) {
274288
if (isAsyncIterator(source)) {
275289
// Workaround for https://github.com/node-fetch/node-fetch/issues/766
276-
if (Object.prototype.hasOwnProperty.call(source, 'readable') && Object.prototype.hasOwnProperty.call(source, 'writable')) {
290+
if (
291+
Object.prototype.hasOwnProperty.call(source, 'readable') &&
292+
Object.prototype.hasOwnProperty.call(source, 'writable')
293+
) {
277294
const iter = source[Symbol.asyncIterator]()
278295

279296
const wrapper = {
@@ -310,11 +327,13 @@ const streamToAsyncIterator = function (source) {
310327
}
311328
}
312329

313-
const isAsyncIterator = (obj) => {
314-
return typeof obj === 'object' &&
315-
obj !== null &&
316-
// typeof obj.next === 'function' &&
317-
typeof obj[Symbol.asyncIterator] === 'function'
330+
const isAsyncIterator = obj => {
331+
return (
332+
typeof obj === 'object' &&
333+
obj !== null &&
334+
// typeof obj.next === 'function' &&
335+
typeof obj[Symbol.asyncIterator] === 'function'
336+
)
318337
}
319338

320339
HTTP.HTTPError = HTTPError
@@ -354,6 +373,7 @@ HTTP.delete = (resource, options) => new HTTP(options).delete(resource, options)
354373
* @param {APIOptions} options
355374
* @returns {Promise<Response>}
356375
*/
357-
HTTP.options = (resource, options) => new HTTP(options).options(resource, options)
376+
HTTP.options = (resource, options) =>
377+
new HTTP(options).options(resource, options)
358378

359379
module.exports = HTTP

src/text-encoder.browser.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
'use strict'
22

3-
module.exports = require('./globalthis').TextDecoder
3+
module.exports = require('./globalthis').TextEncoder

src/text-encoder.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
'use strict'
2-
module.exports = require('util').TextDecoder
2+
module.exports = require('util').TextEncoder

0 commit comments

Comments
 (0)