GithubHelp home page GithubHelp logo

dint-dev / universal_io Goto Github PK

View Code? Open in Web Editor NEW
95.0 4.0 11.0 1.02 MB

Cross-platform 'dart:io', including browser-compatible HttpClient.

Home Page: https://pub.dev/packages/universal_io

License: Apache License 2.0

Dart 100.00%
dart flutter

universal_io's Introduction

Pub Package package publisher Github Actions CI

Overview

A cross-platform dart:io that works on all platforms, including browsers.

You can simply replace dart:io imports with package:universal_io/io.dart.

Licensed under the Apache License 2.0. Some of the source code was derived from Dart SDK, which was obtained under the BSD-style license of Dart SDK. See LICENSE file for details.

APIs added on top of "dart:io"

Other features

The following features may be deprecated in the future versions (3.x) of the package:

  • HttpClient
    • HttpClient() factory is changed so that it returns BrowserHttpClient on browsers.
  • Platform
    • The package makes methods like Platform.isAndroid and Platform.isMacOS work in the browsers too.
  • InternetAddress
    • The package makes it works in the browsers too.
  • BytesBuilder
    • The package makes it works in the browsers too.

Links

Similar packages

Getting started

1.Add dependency

In pubspec.yaml:

dependencies:
  universal_io: ^2.2.2

2.Use HTTP client

import 'package:universal_io/io.dart';

Future<void> main() async {
  // HttpClient can be used in browser too!
  HttpClient httpClient = newUniversalHttpClient(); // Recommended way of creating HttpClient.
  final request = await httpClient.getUrl(Uri.parse("https://dart.dev/"));
  final response = await request.close();
}

HTTP client behavior

HTTP client is implemented with XMLHttpRequest (XHR) API on browsers.

XHR causes the following differences with dart:io:

  • HTTP connection is created only after request.close() has been called.
  • Same-origin policy limitations. For making cross-origin requests, see documentation below.

Helpful error messages

When requests fail and assertions are enabled, error messages contains descriptions how to fix possible issues such as missing cross-origin headers.

The error messages look like the following:

XMLHttpRequest error.
-------------------------------------------------------------------------------
HTTP method:             PUT
HTTP URL:                http://destination.com/example
Origin:                  http://source.com
Cross-origin:            true
browserCredentialsMode:  false
browserResponseType:     arraybuffer

THE REASON FOR THE XHR ERROR IS UNKNOWN.
(For security reasons, browsers do not explain XHR errors.)

Is the server down? Did the server have an internal error?

Enabling credentials mode would enable use of some HTTP headers in both the
request and the response. For example, credentials mode is required for
sending/receiving cookies. If you think you need to enable 'credentials mode',
do the following:

    final httpClientRequest = ...;
    if (httpClientRequest is BrowserHttpClientRequest) {
      httpClientRequest.browserCredentialsMode = true;
    }

Did the server respond to a cross-origin "preflight" (OPTIONS) request?

Did the server send the following headers?
  * Access-Control-Allow-Origin: http://source.com
    * You can also use wildcard ("*").
    * Always required for cross-origin requests!
  * Access-Control-Allow-Methods: PUT
    * You can also use wildcard ("*").

Sometimes when you do cross-origin requests in browsers, you want to use CORS "credentials mode". This can be achieved with the following pattern:

Future<void> main() async {
    final client = HttpClient();
    final request = client.getUrl(Url.parse('http://example/url'));

    // Enable credentials mode
    if (request is BrowserHttpClientRequest) {
      request.browserCredentialsMode = true;
    }

    // Close request
    final response = await request.close();
    // ...
}

Streaming text responses

The underlying XMLHttpRequest (XHR) API supports response streaming only when responseType is "text".

This package automatically uses responseType "text" based on value of the HTTP request header "Accept". These media types are defined BrowserHttpClient.defaultTextMimes:

  • "text/*" (any text media type)
  • "application/grpc-web"
  • "application/grpc-web+proto"

If you want to disable streaming, use the following pattern:

Future<void> main() async {
    final client = newUniversalHttpClient();
    if (client is BrowserHttpClient) {
      client.textMimes = const {};
    }
    // ...
}

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.