Code Monkey home page Code Monkey logo

sync-fetch's Introduction

sync-fetch

Synchronous wrapper around the Fetch API. Uses node-fetch under the hood, and for some input-parsing code and test cases too.

npm

Install

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"></script>

Use

const fetch = require('sync-fetch')

const metadata = fetch('https://doi.org/10.7717/peerj-cs.214', {
  headers: {
    Accept: 'application/vnd.citationstyles.csl+json'
  }
}).json()
// json(), arrayBuffer(), text() and buffer() supported

Limitations

Node.js

  • Does not support Streams (or FormData) as input bodies since they cannot be read or serialized synchronously
  • Does not support Blobs as input bodies since they're too complex
  • Does not support the non-spec agent option as its value cannot be serialized

Browser

  • Does not support most options, since XMLHttpRequest is pretty limited. Supported are:
    • method
    • body
    • headers
    • credentials (but not omit)
    • (Non-spec) timeout
  • The non-standard buffer() method is not supported
  • CORS limitations apply, of course (note they may be stricter for synchronous requests)

sync-fetch's People

Contributors

da70 avatar dependabot[bot] avatar fossprime avatar larsgw avatar mzdunek93 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

sync-fetch's Issues

Commas are being inserted into request string when request length is longer than one chunk

The code in worker.js that reads in the user's HTTP request has a bug in it that causes commas to be inserted in between chunks of the message -- https://github.com/larsgw/sync-fetch/blob/ccf55a4f4c18b04d59ea9dca2dd1a86a83f08f20/worker.js#L14-#L15:

process.stdin.on('end', function () {
  const input = JSON.parse(chunks.join())

Array.prototype.join will use a comma for the separator if no separator is specified. In order to join the array elements into a string without any separator, an empty string must be passed in as the separator argument:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/join:

separator Optional
Specifies a string to separate each pair of adjacent elements of the array. The separator is converted to a string if necessary. If omitted, the array elements are separated with a comma (","). If separator is an empty string, all elements are joined without any characters in between them.

I will submit a PR. Just wanted to open this ticket for reference.

Consider using TypeScript?

Hello! ๐Ÿ‘‹

I've used this library in some of my Web Worker projects to download things synchronously, particularly for WASM magic stuff (sync httpGet() is nice!). I'd be interested in contributing code to this library, but I wanted to check-in with @larsgw, the author, before I do anything so I don't end up with a dead-end fork that only I use ๐Ÿ˜œ

Specifically, here's some things that I'd love โค๏ธ to contribute:

  • TypeScript! ๐Ÿ™Œ And use multiple files.
  • Use web-focused TextEncoder or similar instead of buffer (looks like someone else is already on this in #27! nice!)
  • Tooling and stuff for a modern bundler like Vite or tsup
  • An improved readme with: more example code, example use case (web worker), more usage details, etc.
  • Refactor tests into multiple files.
  • Use ESM modules (possibly still compiled to CommonJS)

If you'd like to keep it steady and stable as-is, that's OK too! ๐Ÿ˜Š I'm just reaching out to strike up a discussion about whether or not my PRs would be stuck in limbo ๐Ÿ™„

Stream body to stdout in worker

Instead of using JSON.stringify and console.log to pass the response & body back to the main process, only write the headers & response and then stream the body.

See #30

Check out `await-sync` - replace child process with WebWorkers

I just created this async-to-sync package today that i named await-sync

it utilize web workers instead of spawning sync processes and data is transfered synchronous over SharedArrayBuffer so less code needs to be copied over.

Therefore it's also more compatible with other enviorments like Deno, Bun, and also Web Workers.

it's as simple as just doing:

import { createWorker } from 'to-sync'

const awaitSync = createWorker()

const fetchSync = awaitSync(async function (...args) {
  const res = await fetch(...args)
  const ab = await res.arrayBuffer()
  return new Uint8Array(ab)
})

const uint8 = fetchSync(url)
const text = new TextDecoder().decode(uint8)
const json = JSON.parse(text)
const blob = new Blob([uint8])

if you would use this instead then your package would have the possibility of being able to run in Deno, Bun.js and also Web Workers. but why web workers when you sync xhr...? that's a good question...

Produces incorrect data with .buffer()

Calling fetch on a URL and then calling .buffer() on the response produces incorrect data. Notice how sync-fetch's .buffer() begins with ef while synchronous-fetch and node-fetch's .buffer() both begin with 89.

Using the following versions of these packages:
"node-fetch": "^2.6.5",
"sync-fetch": "^0.3.0",
"synchronous-fetch": "^1.0.0"

> require('synchronous-fetch')('https://www.google.com/images/branding/googlelogo/1x/googlelogo_light_color_272x92dp.png').buffer().body
<Buffer 89 50 4e 47 0d 0a 1a 0a 00 00 00 0d 49 48 44 52 00 00 01 10 00 00 00 5c 08 04 00 00 00 0c ee 22 3d 00 00 0d 74 49 44 41 54 78 01 ed 9d 79 94 55 d5 99 ... 3451 more bytes>
> require('sync-fetch')('https://www.google.com/images/branding/googlelogo/1x/googlelogo_light_color_272x92dp.png').buffer()
<Buffer ef bf bd 50 4e 47 0d 0a 1a 0a 00 00 00 0d 49 48 44 52 00 00 01 10 00 00 00 5c 08 04 00 00 00 0c ef bf bd 22 3d 00 00 0d 74 49 44 41 54 78 01 ef bf bd ... 6265 more bytes>
> require('node-fetch')("https://www.google.com/images/branding/googlelogo/1x/googlelogo_light_color_272x92dp.png").then( (response) => response.buffer()).then( (response) => console.log(response) )      
Promise { <pending> }
> <Buffer 89 50 4e 47 0d 0a 1a 0a 00 00 00 0d 49 48 44 52 00 00 01 10 00 00 00 5c 08 04 00 00 00 0c ee 22 3d 00 00 0d 74 49 44 41 54 78 01 ed 9d 79 94 55 d5 99 ... 3451 more bytes>

DeprecationWarning: The `punycode` module is deprecated

(node:2941255) [DEP0040] DeprecationWarning: The `punycode` module is deprecated. Please use a userland alternative instead.
    at node:punycode:5:10
    at NativeModule.compileForInternalLoader (node:internal/bootstrap/loaders:312:7)
    at NativeModule.compileForPublicLoader (node:internal/bootstrap/loaders:252:10)
    at loadNativeModule (node:internal/modules/cjs/helpers:49:9)
    at Function.Module._load (node:internal/modules/cjs/loader:804:15)
    at Module.require (node:internal/modules/cjs/loader:1005:19)
    at require (node:internal/modules/cjs/helpers:102:18)
    at Object.<anonymous> (/opt/apps/Adachi-BOT/node_modules/whatwg-url/lib/url-state-machine.js:2:18)
    at Module._compile (node:internal/modules/cjs/loader:1105:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1159:10)
# npm list whatwg-url
[email protected] /opt/apps/Adachi-BOT
โ””โ”€โ”ฌ [email protected]
  โ””โ”€โ”ฌ [email protected]
    โ””โ”€โ”€ [email protected]

jsdom/whatwg-url#191

node-fetch/node-fetch@0959ca9#diff-7ae45ad102eab3b6d7e7896acd08c427a9b25b346470d7bc6507b6481575d519L63

@larsgw Will you support node-fetch 3.x (or newer)?

Updating node-fetch to v3

From #29 (comment):

node-fetch 3 introduces a lot of breaking changes, the most problematic one being that the package has been converted to an ES module which makes it impossible to import synchronously.

My preferred workaround for this would be to only import node-fetch in the worker process, where asynchronous imports do not matter. This however requires writing a replacement implementation for the Response, Request and Header interfaces in the main process.

localhost doesnt seem to work

im trying to fetch data to a local express server for testing and i get this error

FetchError: request to https://0.0.0.0:9000/dual failed, reason: write EPROTO 140361671825344:error:1408F10B:SSL routines:ssl3_get_record:wrong version number:ssl/record/ssl3_record.c:331:

Command failed when used in VS Code plugin

Error mesage:

Command failed: /Applications/Visual Studio Code.app/Contents/MacOS/Electron XXX/node_modules/sync-fetch/worker.js

Can't figure out why this is happening. Took a look at sync-fetch/worker.js and couldn't make any progress.

please update node-fetch to 3 latest.

I'm using yarn and resolutions field in package.json to enforce [email protected] to all other my app dependencies, and sync-fetch is failing, as part of @graphql-tools/apollo-engine-loader@^7.0.5 and/or @graphql-tools/github-loader@^7.0.5

/project/node_modules/sync-fetch/index.js:4
const _fetch = require('node-fetch')
               ^

Error [ERR_REQUIRE_ESM]: require() of ES Module /project/node_modules/node-fetch/src/index.js from /project/node_modules/sync-fetch/index.js not supported.
Instead change the require of /project/node_modules/node-fetch/src/index.js in /project/node_modules/sync-fetch/index.js to a dynamic import() which is available in all CommonJS modules.
    at Object.<anonymous> (/project/node_modules/sync-fetch/index.js:4:16)
    at Object.<anonymous> (/project/node_modules/@graphql-tools/url-loader/index.js:38:43)
    at Object.<anonymous> (/project/node_modules/graphql-config/index.js:14:19)
    at Object.<anonymous> (/project/node_modules/@graphql-codegen/cli/bin.js:42:23) {
  code: 'ERR_REQUIRE_ESM'
}

Trying to keep my dependencies without vulnerabilities in prod.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.