Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Better object checks #673

Merged
merged 4 commits into from Sep 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
44 changes: 2 additions & 42 deletions src/body.js
Expand Up @@ -9,11 +9,12 @@ import Stream from 'stream';

import Blob, {BUFFER} from './blob';
import FetchError from './fetch-error';
import {isBlob, isURLSearchParams} from './utils/is';

let convert;
try {
convert = require('encoding').convert;
} catch (error) {}
} catch (error) { }

const INTERNALS = Symbol('Body internals');

Expand Down Expand Up @@ -341,47 +342,6 @@ function convertBody(buffer, headers) {
).toString();
}

/**
* Detect a URLSearchParams object
* ref: https://github.com/bitinn/node-fetch/issues/296#issuecomment-307598143
*
* @param Object obj Object to detect by type or brand
* @return String
*/
function isURLSearchParams(obj) {
// Duck-typing as a necessary condition.
if (typeof obj !== 'object' ||
typeof obj.append !== 'function' ||
typeof obj.delete !== 'function' ||
typeof obj.get !== 'function' ||
typeof obj.getAll !== 'function' ||
typeof obj.has !== 'function' ||
typeof obj.set !== 'function') {
return false;
}

// Brand-checking and more duck-typing as optional condition.
return obj.constructor.name === 'URLSearchParams' ||
Object.prototype.toString.call(obj) === '[object URLSearchParams]' ||
typeof obj.sort === 'function';
}

/**
* Check if `obj` is a W3C `Blob` object (which `File` inherits from)
* @param {*} obj
* @return {boolean}
*/
function isBlob(obj) {
return typeof obj === 'object' &&
typeof obj.arrayBuffer === 'function' &&
typeof obj.type === 'string' &&
typeof obj.stream === 'function' &&
typeof obj.constructor === 'function' &&
typeof obj.constructor.name === 'string' &&
/^(Blob|File)$/.test(obj.constructor.name) &&
/^(Blob|File)$/.test(obj[Symbol.toStringTag]);
}

/**
* Clone body given Res/Req instance
*
Expand Down
22 changes: 7 additions & 15 deletions src/request.js
Expand Up @@ -12,6 +12,7 @@ import Stream from 'stream';
import utf8 from 'utf8';
import Headers, {exportNodeCompatibleHeaders} from './headers';
import Body, {clone, extractContentType, getTotalBytes} from './body';
import {isAbortSignal} from './utils/is';

const INTERNALS = Symbol('Request internals');

Expand All @@ -22,27 +23,18 @@ const formatUrl = Url.format;
const streamDestructionSupported = 'destroy' in Stream.Readable.prototype;

/**
* Check if a value is an instance of Request.
* Check if `obj` is an instance of Request.
*
* @param Mixed input
* @return Boolean
* @param {*} obj
* @return {boolean}
*/
function isRequest(input) {
function isRequest(obj) {
return (
typeof input === 'object' &&
typeof input[INTERNALS] === 'object'
typeof obj === 'object' &&
typeof obj[INTERNALS] === 'object'
);
}

function isAbortSignal(signal) {
const proto = (
signal &&
typeof signal === 'object' &&
Object.getPrototypeOf(signal)
);
return Boolean(proto && proto.constructor.name === 'AbortSignal');
}

/**
* Request class
*
Expand Down
60 changes: 60 additions & 0 deletions src/utils/is.js
@@ -0,0 +1,60 @@
/**
* Is.js
*
* Object type checks.
*/

const NAME = Symbol.toStringTag;

/**
* Check if `obj` is a URLSearchParams object
* ref: https://github.com/bitinn/node-fetch/issues/296#issuecomment-307598143
*
* @param {*} obj
* @return {boolean}
*/
export function isURLSearchParams(obj) {
return (
typeof obj === 'object' &&
typeof obj.append === 'function' &&
typeof obj.delete === 'function' &&
typeof obj.get === 'function' &&
typeof obj.getAll === 'function' &&
typeof obj.has === 'function' &&
typeof obj.set === 'function' &&
typeof obj.sort === 'function' &&
obj[NAME] === 'URLSearchParams'
);
}

/**
* Check if `obj` is a W3C `Blob` object (which `File` inherits from)
*
* @param {*} obj
* @return {boolean}
*/
export function isBlob(obj) {
return (
typeof obj === 'object' &&
typeof obj.arrayBuffer === 'function' &&
typeof obj.type === 'string' &&
typeof obj.stream === 'function' &&
typeof obj.constructor === 'function' &&
/^(Blob|File)$/.test(obj[NAME])
);
Richienb marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* Check if `obj` is an instance of AbortSignal.
*
* @param {*} obj
* @return {boolean}
*/
export function isAbortSignal(obj) {
return (
obj &&
typeof obj === 'object' &&
obj[NAME] === 'AbortSignal'
);
}