Skip to content

Commit

Permalink
fix(blob): Allow for Request object to be a body of objects (#767)
Browse files Browse the repository at this point in the history
* fix(blob): Allow for Request object to be a body of objects

Before this commit, you could not pass Request objects because of a bug in the
module we used to detect plain objects. This is now fixed.

* lockfile
  • Loading branch information
vvo authored Oct 2, 2024
1 parent d7b5153 commit da87e89
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 14 deletions.
5 changes: 5 additions & 0 deletions .changeset/pretty-tomatoes-jump.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@vercel/blob': patch
---

Fix bad detection of Request being a plain object
1 change: 0 additions & 1 deletion packages/blob/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@
"async-retry": "^1.3.3",
"bytes": "^3.1.2",
"is-buffer": "^2.0.5",
"is-plain-object": "^5.0.0",
"undici": "^5.28.4"
},
"devDependencies": {
Expand Down
18 changes: 18 additions & 0 deletions packages/blob/src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,21 @@ export function getDownloadUrl(blobUrl: string): string {

return url.toString();
}

// Extracted from https://github.com/sindresorhus/is-plain-obj/blob/main/index.js
// It's just nearly impossible to use ESM modules with our current setup
export function isPlainObject(value: unknown): boolean {
if (typeof value !== 'object' || value === null) {
return false;
}

// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment -- ok
const prototype = Object.getPrototypeOf(value);
return (
(prototype === null ||
prototype === Object.prototype ||
Object.getPrototypeOf(prototype) === null) &&
!(Symbol.toStringTag in value) &&
!(Symbol.iterator in value)
);
}
7 changes: 5 additions & 2 deletions packages/blob/src/multipart/create-uploader.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { isPlainObject } from 'is-plain-object';
import { BlobError, type CommonCreateBlobOptions } from '../helpers';
import {
BlobError,
isPlainObject,
type CommonCreateBlobOptions,
} from '../helpers';
import type { CreatePutMethodOptions, PutBody } from '../put-helpers';
import { createPutHeaders, createPutOptions } from '../put-helpers';
import { completeMultipartUpload } from './complete';
Expand Down
2 changes: 1 addition & 1 deletion packages/blob/src/multipart/upload.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import bytes from 'bytes';
import type { BodyInit } from 'undici';
import { isPlainObject } from 'is-plain-object';
import { BlobServiceNotAvailable, requestApi } from '../api';
import { debug } from '../debug';
import {
type CommonCreateBlobOptions,
type BlobCommandOptions,
BlobError,
isPlainObject,
} from '../helpers';
import { createPutHeaders, createPutOptions } from '../put-helpers';
import type { PutBody, CreatePutMethodOptions } from '../put-helpers';
Expand Down
3 changes: 1 addition & 2 deletions packages/blob/src/put.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import type { BodyInit } from 'undici';
import { isPlainObject } from 'is-plain-object';
import { requestApi } from './api';
import type { CommonCreateBlobOptions } from './helpers';
import { BlobError } from './helpers';
import { BlobError, isPlainObject } from './helpers';
import { uncontrolledMultipartUpload } from './multipart/uncontrolled';
import type {
CreatePutMethodOptions,
Expand Down
8 changes: 0 additions & 8 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit da87e89

Please sign in to comment.