Skip to content

Commit

Permalink
fix: make fetch's signature spec-compliant (#1226)
Browse files Browse the repository at this point in the history
  • Loading branch information
targos committed Feb 12, 2022
1 parent db745e4 commit 3e267ec
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 47 deletions.
4 changes: 2 additions & 2 deletions index.js
Expand Up @@ -94,12 +94,12 @@ module.exports.getGlobalDispatcher = getGlobalDispatcher

if (nodeMajor > 16 || (nodeMajor === 16 && nodeMinor >= 5)) {
let fetchImpl = null
module.exports.fetch = async function fetch (resource, init) {
module.exports.fetch = async function fetch (resource) {
if (!fetchImpl) {
fetchImpl = require('./lib/fetch')
}
const dispatcher = getGlobalDispatcher()
return fetchImpl.call(dispatcher, resource, init)
return fetchImpl.apply(dispatcher, arguments)
}
module.exports.Headers = require('./lib/fetch/headers').Headers
module.exports.Response = require('./lib/fetch/response').Response
Expand Down
53 changes: 8 additions & 45 deletions test/fetch/client-fetch.js
Expand Up @@ -8,55 +8,18 @@ const { ReadableStream } = require('stream/web')
const { Blob } = require('buffer')
const { fetch, Response, Request, FormData, File, FileLike } = require('../..')

test('args validation', (t) => {
t.plan(12)

t.throws(() => {
File.prototype.name.call(null)
}, TypeError)
t.throws(() => {
File.prototype.lastModified.call(null)
}, TypeError)
t.throws(() => {
File.prototype[Symbol.toStringTag].call(null)
}, TypeError)

t.throws(() => {
FileLike.prototype.stream.call(null)
}, TypeError)
t.throws(() => {
FileLike.prototype.arrayBuffer.call(null)
}, TypeError)
t.throws(() => {
FileLike.prototype.slice.call(null)
}, TypeError)
t.throws(() => {
FileLike.prototype.text.call(null)
}, TypeError)
t.throws(() => {
FileLike.prototype.size.call(null)
}, TypeError)
t.throws(() => {
FileLike.prototype.type.call(null)
}, TypeError)
t.throws(() => {
FileLike.prototype.name.call(null)
}, TypeError)
t.throws(() => {
FileLike.prototype.lastModified.call(null)
}, TypeError)
t.throws(() => {
FileLike.prototype[Symbol.toStringTag].call(null)
}, TypeError)
test('function signature', (t) => {
t.plan(2)

t.equal(fetch.name, 'fetch')
t.equal(fetch.length, 1)
})

test('return value of File.lastModified', (t) => {
test('args validation', async (t) => {
t.plan(2)

const f = new File(['asd1'], 'filename123')
const lastModified = f.lastModified
t.ok(typeof lastModified === typeof Date.now())
t.ok(lastModified >= 0 && lastModified <= Date.now())
await t.rejects(fetch(), TypeError)
await t.rejects(fetch('ftp://unsupported'), TypeError)
})

test('request json', (t) => {
Expand Down
51 changes: 51 additions & 0 deletions test/fetch/file.js
Expand Up @@ -3,6 +3,57 @@
const { test } = require('tap')
const { File, FileLike } = require('../../lib/fetch/file')

test('args validation', (t) => {
t.plan(12)

t.throws(() => {
File.prototype.name.call(null)
}, TypeError)
t.throws(() => {
File.prototype.lastModified.call(null)
}, TypeError)
t.throws(() => {
File.prototype[Symbol.toStringTag].call(null)
}, TypeError)

t.throws(() => {
FileLike.prototype.stream.call(null)
}, TypeError)
t.throws(() => {
FileLike.prototype.arrayBuffer.call(null)
}, TypeError)
t.throws(() => {
FileLike.prototype.slice.call(null)
}, TypeError)
t.throws(() => {
FileLike.prototype.text.call(null)
}, TypeError)
t.throws(() => {
FileLike.prototype.size.call(null)
}, TypeError)
t.throws(() => {
FileLike.prototype.type.call(null)
}, TypeError)
t.throws(() => {
FileLike.prototype.name.call(null)
}, TypeError)
t.throws(() => {
FileLike.prototype.lastModified.call(null)
}, TypeError)
t.throws(() => {
FileLike.prototype[Symbol.toStringTag].call(null)
}, TypeError)
})

test('return value of File.lastModified', (t) => {
t.plan(2)

const f = new File(['asd1'], 'filename123')
const lastModified = f.lastModified
t.ok(typeof lastModified === typeof Date.now())
t.ok(lastModified >= 0 && lastModified <= Date.now())
})

test('Symbol.toStringTag', (t) => {
t.plan(2)
t.equal(new File()[Symbol.toStringTag], 'File')
Expand Down

0 comments on commit 3e267ec

Please sign in to comment.