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

Support the HTTP package on nodejs platform #1126

Open
bramp opened this issue Feb 1, 2024 · 3 comments
Open

Support the HTTP package on nodejs platform #1126

bramp opened this issue Feb 1, 2024 · 3 comments
Labels
package:http type-enhancement A request for a change that isn't a bug

Comments

@bramp
Copy link

bramp commented Feb 1, 2024

I'm not sure if this is a bug, or a feature request. We use nodejs to test our projects when compiled with dart2js, but we noticed that the HTTP APIs don't seem to work on nodejs, but do within Chrome.

Take for example

import 'package:test/test.dart';
import 'package:http/http.dart' as http;

void main() {
  test('Simple http get', () async {
    final client = http.Client();

    final response = await client.get(Uri.http('localhost:8080'));

    expect(response.statusCode, 200);
  });
}

Dart VM

$ dart test test/node_test.dart
00:00 +1: All tests passed!

Chrome
Make sure your webserver returns a appropriate CORS header, e.g "Access-Control-Allow-Origin: *"

$ dart test test/node_test.dart -p chrome
00:03 +1: All tests passed!

Node (which fails)

$ dart test test/node_test.dart -p node

00:03 +0 -1: Simple http get [E]
  Error: self.XMLHttpRequest is not a constructor
  org-dartlang-sdk:///lib/_internal/js_shared/lib/js_util_patch.dart 307:10  <fn>
  org-dartlang-sdk:///lib/_internal/js_runtime/lib/async_patch.dart 307:19   _wrapJsFunctionForAsync.closure.$protected
  org-dartlang-sdk:///lib/_internal/js_runtime/lib/async_patch.dart 332:23   _wrapJsFunctionForAsync.<fn>
  org-dartlang-sdk:///lib/_internal/js_runtime/lib/async_patch.dart 283:19   _awaitOnObject.<fn>
  org-dartlang-sdk:///lib/async/zone.dart 1407:46                            StaticClosure._rootRunUnary
  org-dartlang-sdk:///lib/async/zone.dart 1307:34                            _CustomZone.runUnary
  org-dartlang-sdk:///lib/async/future_impl.dart 127:29                      _Future._propagateToListeners.handleValueCallback
  org-dartlang-sdk:///lib/async/future_impl.dart 875:13                      Object._Future._propagateToListeners
  org-dartlang-sdk:///lib/async/future_impl.dart 647:5                       _Future._completeWithValue
  org-dartlang-sdk:///lib/async/future_impl.dart 721:7                       _Future._asyncCompleteWithValue.<fn>
@bramp bramp added package:http type-enhancement A request for a change that isn't a bug labels Feb 1, 2024
@Zekfad
Copy link

Zekfad commented Feb 12, 2024

If you're using node v18+ you can try fetch instead of XHR
Ref: https://nodejs.org/dist/latest/docs/api/globals.html#fetch

@bramp
Copy link
Author

bramp commented Feb 12, 2024

Thanks Zekfad, if that's addressed to me, I don't control how the 'package:http/http.dart' package works internally, but it should be agnostic to the platform dart is running on. As a workaround, that may be suitable suggestion, but for now, I've just switched all our testing to the Chrome browser.

@Zekfad
Copy link

Zekfad commented Feb 12, 2024

You can use userland client implementation, e.g. fetch_client uses fetch api to implement http client. That in theory should work fine, if node is 18+, with global fetch function.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
package:http type-enhancement A request for a change that isn't a bug
Projects
None yet
Development

No branches or pull requests

2 participants