pwnall / node-xhr2

XMLHttpRequest emulator for node.js
MIT License
104 stars 41 forks source link

Synchronous XHR processing not implemented #19

Open retorquere opened 9 years ago

retorquere commented 9 years ago

I'm trying to d a synchronous POST, but I'm getting "Synchronous XHR processing not implemented". code:

      req = new XMLHttpRequest()
      req.open('POST', 'http://localhost:23119/better-bibtex/schomd', false)
      req.send(body)
pwnall commented 9 years ago

@retorquere I looked into doing it, and everything I could find is really ugly. node's http libraries don't support synchronous requests, so I'd have to do a synchronous spawn and do all the processing in a child. This is a lot of work for a feature that most Web platform folks want deprecated.

On the bright side, there seem to be a few libraries doing synchronous HTTP, and that wasn't the case last time I looked into implementing synchronous XHR.

retorquere commented 9 years ago

I've found some, thanks. For my case, I don't see a way around synchronous.

mhnsn commented 5 years ago

@retorquere Do you happen to have any recommendations? I'm working on a CLI app, and asynchronous behavior is breaking core functionality.

jcde commented 4 years ago

xmlhttprequest package supports synchronous yet

VirtualTim commented 4 years ago

Ran into a painful debugging scenario before I realised that this module doesn't support sync XMLHttpRequest's. I might open a PR to at least throw an error if someone attempts to use this, to make debugging easier.

jimmywarting commented 1 year ago

I created to-sync today

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 toSync = createWorker()

const fetchSync = toSync(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])

check it out... it's very easy to use. all the arguments is easily transferable over to the worker cuz it can use structural clone alg instead of passing things around with argv flags. or over stdin/out