Skip to content

Commit

Permalink
refactor!: replace Buffer with TextEncoder and Uint8Array
Browse files Browse the repository at this point in the history
Close #27

BREAKING CHANGE: Support for IE and Edge <18 dropped.

Co-authored-by: Arda TANRIKULU <ardatanrikulu@gmail.com>
  • Loading branch information
larsgw and ardatan committed Jun 11, 2023
1 parent 18f2c3d commit 1615baa
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 31 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ Synchronous wrapper around the Fetch API. Uses [`node-fetch`](https://github.com

npm install sync-fetch

In the browser, a browserify bundle can be loaded from CDNs like unpkg.com.

<script src="https://unpkg.com/sync-fetch"></script>
<script src="https://unpkg.com/sync-fetch@VERSION/browser.js"></script>

## Use

```js
Expand Down
22 changes: 16 additions & 6 deletions browser.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-env browser */

const { Buffer } = require('buffer/')
const textEncoder = new TextEncoder()

function syncFetch (...args) {
const request = new syncFetch.Request(...args)
Expand Down Expand Up @@ -36,7 +36,7 @@ function syncFetch (...args) {

let body = xhr.response
if (useBinaryEncoding) {
const buffer = Buffer.alloc(body.length)
const buffer = new Uint8Array(body.length)
for (let i = 0; i < body.length; i++) {
buffer[i] = body.charCodeAt(i) & 0xff
}
Expand Down Expand Up @@ -116,7 +116,7 @@ class SyncRequest {
this[INTERNALS] = {
method: init.method || 'GET',
headers: new syncFetch.Headers(init.headers),
body: init.body ? Buffer.from(init.body) : null,
body: parseBody(init.body),
credentials: init.credentials || 'omit',

// Non-spec
Expand Down Expand Up @@ -201,7 +201,7 @@ class SyncRequest {
class SyncResponse {
constructor (body, init = {}) {
this[INTERNALS] = {
body: body ? Buffer.from(body) : null,
body: parseBody(body),
bodyUsed: false,

headers: new syncFetch.Headers(init.headers),
Expand Down Expand Up @@ -258,7 +258,7 @@ class SyncResponse {
class Body {
constructor (body) {
this[INTERNALS] = {
body: Buffer.from(body),
body: parseBody(body),
bodyUsed: false
}
}
Expand Down Expand Up @@ -311,7 +311,17 @@ function checkBody (body) {
function consumeBody (body) {
checkBody(body)
body[INTERNALS].bodyUsed = true
return body[INTERNALS].body || Buffer.alloc(0)
return body[INTERNALS].body || new Uint8Array()
}

function parseBody (body) {
if (typeof body === 'string') {
return textEncoder.encode(body)
} else if (body) {
return body
} else {
return null
}
}

Body.mixin(SyncRequest.prototype)
Expand Down
26 changes: 2 additions & 24 deletions package-lock.json

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

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
},
"homepage": "https://github.com/larsgw/sync-fetch#readme",
"dependencies": {
"buffer": "^5.7.1",
"node-fetch": "^2.6.1"
},
"devDependencies": {
Expand Down

0 comments on commit 1615baa

Please sign in to comment.