p-hatan/ mirror

pub repository

mirror.p-hatan.com

universal_io

2.0.0-nullsafety.1

Cross-platform 'dart:io' that works in all platforms (browsers, Flutter, and VM).

Download archive

Readme

Pub Package Github Actions CI

Overview

A cross-platform dart:io that works in all platforms (browsers, Flutter, and VM).

The API is exactly the same API as dart:io. You can simply replace dart:io imports with package:universal_io/io.dart.

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

Links

Similar packages

Getting started

pubspec.yaml

dependencies:
  universal_io: ^2.0.0-nullsafety.1

main.dart

import 'package:universal_io/io.dart';

Future<void> main() async {
  // HttpClient can be used in browser too!
  final httpClient = HttpClient();
  final request = await httpClient.getUrl(Uri.parse("http://example/url"));
  final response = await request.close();
}

Browser driver

HTTP client

HTTP client is implemented with XMLHttpRequest (XHR) (in dart:html, the class is HttpRequest).

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();
    // ...
}

See source code.

Streaming text responses

The underlying XMLHttpRequest (XHR) API supports response streaming only when responseType is "text". This package automatically uses responseType "text" in some cases based on value of the HTTP request header "Accept".

If you want to always have arraybuffer / text responses, use:

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

    // The following causes XHR responseType 'text' to be used when request is closed.
    request.headers.set('Accept', 'text/plain');

    // Use XHR responseType 'arraybuffer'.
    if (request is BrowserHttpClientRequest) {
      request.browserResponseType = 'arrayBuffer';
    }

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

See source code.

Platform

Supported Platform APIs include:

  • Platform.isWindows
  • Platform.operatingSystem
  • Platform.locale

See source code.

Changelog

2.0.0-nullsafety.1

  • Improves documentation.
  • Improves BrowserHttpClientException messages.
  • Deprecates libraries prefer_sdk/io.dart and prefer_universal/io.dart. Developers should import just io.dart.

2.0.0-nullsafety.0

  • The first null-safe version.
  • Makes changes in BrowserHttpClient / BrowserHttpClientRequest API:
    • The property for enabling credentials mode is now browserCredentialsMode. The default is false.
    • The property for setting response type is now browserResponseType ("arraybuffer", "text", etc.). By default, if HTTP request header "Accept" contains only text MIMEs ("text/plain", etc.), this package uses responseType "text".
    • HTTP client now has onBrowserHttpClientRequestClose for using your own logic for setting browserResponseType.
  • Removes IO adapter API.

1.0.2

  • Fixes issue #11 (InternetAddress parameter).
  • Fixes issue #12 (CORS credentials mode). Eliminates legacy, complicated behavior. Developers should choose either omit or include. Improves error messages and documentation related to it.
  • Replaces MD5/SHA1 implementations used by some of the source code copied from dart:io. It now uses package:crypto instead of implementations copied from dart:io.

1.0.0

  • Implements recent changes in 'dart:io' (Dart SDK 2.8).
  • HttpDriver is replaced by 'dart:io' HttpOverrides.
  • FileSystemDriver is replaced by 'dart:io' IOOverrides.
  • Various other driver APIs are renamed or removed.
  • BrowserLikeHttpClientRequest is now BrowserHttpClientRequest.
  • BrowserHttpClientRequest implementation is improved.

0.8.6

  • Fixed documentation and small fixes related to nodejs_io.

0.8.5

  • Raised minimum SDK to 2.6 and upgraded dependencies.
  • Changed how CORS credentials mode is enabled. It was previously enabled with a header, but now we introduced subclasses for HttpClient and HttpClientRequest. This is a breaking change, but we decided not to bump the major version number.
  • Improved analysis and test settings.

0.8.4

  • Added 'prefer_sdk/io.dart' and 'prefer_universal/io.dart' libraries for dealing with conditional export issues.
  • Library 'package:universal_io/io.dart' now exports SDK version by default.

0.8.3

  • Replaced IP address parsing with the new Uri.parseIPv4Address / Uri.parseIPv6Address.
  • Fixed missing HTTP status codes.

0.8.2

  • Fixed problems introduced by Dart SDK 2.5.0-dev-2.0.

0.8.1

  • Fixed pubspec.yaml and documented Dart SDK 2.5 breaking changes.

0.8.0

  • Updated classes to Dart 2.5. See Dart SDK documentation about the changes.
    • Various APIs now return Uint8List instead of List<int>. Examples: File, Socket, HttpClientResponse.
    • Various other breaking changes such as Cookie constructor.

0.7.3

  • Fixed the following error thrown by the Dart build system in some cases: "Unsupported conditional import of dart:io found in universal_io|lib/io.dart".

0.7.2

  • Small fixes.

0.7.1

  • Fixed various bugs.
  • Improved the test suite.

0.7.0

  • Improved driver base classes and the test suite.

0.6.0

  • Major refactoring of IODriver API.

0.5.1

  • Fixed small bugs.

0.5.0

  • Fixed various bugs.
  • Re-organized source code.
  • Eliminated dependencies by doing IP parsing in this package.
  • Improved the test suite for drivers.

Installing

export PUB_HOSTED_URL=https://pub.mirror.p-hatan.com
flutter pub add universal_io
pubspec.yaml
name: universal_io
version: 2.0.0-nullsafety.1
description: Cross-platform 'dart:io' that works in all platforms (browsers, Flutter, and VM).
homepage: https://github.com/dart-io-packages/universal_io

environment:
  sdk: '>=2.12.0-133.2 <3.0.0'

dependencies:
  collection: ^1.15.0-nullsafety.4
  crypto: ^3.0.0-nullsafety.0
  meta: ^1.3.0-nullsafety.6
  typed_data: ^1.3.0-nullsafety.5
  zone_local: ^0.2.0-nullsafety.0

dev_dependencies:
  async: ^2.5.0-nullsafety.3
  pedantic: ^1.10.0-nullsafety.3
  stream_channel: ^2.1.0-nullsafety.3
  test: ^1.16.0-nullsafety.13

#dependency_overrides:
#  crypto: ^3.0.0-nullsafety.0

Version history

VersionPublishedArchiveSHA-256
0.0.12018-12-04T19:37:19.011323ZDownloada8aa4b4fa0ed2e298ae4620f1a963b19ddd4cb7a385b65f02b79421d5c6f259b
0.1.02019-02-18T13:57:26.157075ZDownloadadeca9748c95d713e9b244d5278c69155cac14e368e002dc5f01e2f421128b76
0.1.12019-02-18T21:46:46.616626ZDownloadb4d0cce4eaf430212064bb594e4213a4ead522acc5242b921d219c9efaefe2b0
0.2.02019-05-31T10:54:11.421350ZDownloaddebb79ed9c47f7a90973d0aae77bcd01bd8e0e66156412324f37399f2336d4ee
0.3.02019-05-31T13:27:13.026878ZDownload0b8f764b4e55e89c26c5de3665c15d7c30818ec14b6b37763cfd8ee298a054e5
0.4.02019-05-31T23:56:54.513127ZDownload7f879f7900304c5e9465d44f948538202d74179263adc6d7865e83cdd90ee63f
0.5.02019-06-08T17:15:40.346705ZDownloada185c8ca630a62f86e30565576457a96e22581d406c248c469ad95a6cdf5f7c2
0.5.12019-06-08T20:40:49.193850ZDownload47881238750e5a875236a7e39fd9043669442cc0858f0c05b90776a2c8a13ab3
0.6.02019-06-09T15:14:44.044116ZDownload0465c285afc06a6bae58204d3c5e4b970cb040108e777424b3bf2616be30fb21
0.7.02019-06-10T15:43:12.255070ZDownload0b6ff36a76b7e862284ba5ffc1fd189b47108e7561ccbf374f047e9b0fc988ef
0.7.12019-06-11T12:49:21.293001ZDownload3fd33123521fdb09daa588bb86f18f164b9f49f3f37515ef05b665a2c3e947d1
0.7.22019-06-11T14:57:37.911870ZDownloadab9168079199cd23c4b89e2fd77680d34ecab479f84561da16b09c9182e30f36
0.7.32019-07-08T22:24:43.021295ZDownload9918c2ae9eed388c2e0a97e25480a2a1e715de4ed6f24137f8c2b30433d6e492
0.8.02019-07-24T05:43:38.735341ZDownload9e971384b0d5dbf92e477b9f26bc474fe7e4ebd913bfa29473f6063a9b0ffa98
0.8.12019-07-24T17:05:10.369224ZDownload39dd369c4d8eba7d503d2c3856affc8308b0c43412f3fc84405da58ad9e29abc
0.8.22019-09-07T18:05:10.328141ZDownload873f9c3e25e7248df42b483d890caa87a03722467161592bfa341543bdb5629f
0.8.32019-09-08T19:32:12.653442ZDownload0da2059835e189a63058523e5055ecd659b17d187c1a5d23285709d963979fb2
0.8.42019-09-09T19:42:23.264520ZDownloadb1c843018cd4e21b8c551c991ca6ddab6c23538aaf377cd91fb3cee90a3b7ec0
0.8.52019-12-18T22:57:29.460913ZDownload50a660f95189e042ea5df3080302f6513a28139ba036ff92185d9705fa874883
0.8.62020-01-08T11:27:09.248339ZDownloade700bb7cc12768429241339f16afbb155a3897846b16f11476c6c36431986502
1.0.02020-02-17T21:02:40.451806ZDownload5da439c3cee498d1facb919b4bf3c519155640ad17cf10d572f237df86bddf21
1.0.12020-02-24T10:57:05.544932ZDownload2c0fa418dfe4758c36171f228c68f80cbe624bc382872a907f6963618bf6cb08
1.0.22021-01-02T22:39:40.952162ZDownload22c5875f15a25dd8b916566757589dff6c44bd355dcfc0f2e376d9bab7f9e8db
2.0.0-nullsafety.02021-01-11T00:50:59.258777ZDownload2fe15fcace7078c5a20ce680f748add1fdc3de2ec26f0799fd2f9632cac8acd3
2.0.0-nullsafety.12021-01-11T02:07:11.187443ZDownload2c94fd97e48e52902f875fb60cd4337c9e4abdd4ac15d72e79205e8bf8a42cfa
2.0.0-nullsafety.22021-01-11T02:31:17.910536ZDownloadad7e7015e83e6bbb5ec6fce16cea7f484a0d3751069fc0fb314829a4288099d1
2.0.02021-03-04T21:27:30.040176ZDownload6ba43dc871719171a77114b308729bb92e3d9bd259717fe3cf5e1b6cc175397e
2.0.12021-03-15T02:08:40.354623ZDownloadffb7b33ee95da5d5c6edc697d7c79f4ea72690b527b327fb79f48dea2921e456
2.0.22021-03-25T23:43:13.546427ZDownload024376826b2df3d68929afa5b4805678905ee29e8c6a9390d15655c9082d832b
2.0.32021-03-26T00:36:10.989788ZDownloadee2ecc7c9a9817588ccde39d91beb00c763d435735ba43dac2e252da98bc6f23
2.0.42021-04-02T20:15:10.594231ZDownload79f78ddad839ee3aae3ec7c01eb4575faf0d5c860f8e5223bc9f9c17f7f03cef
2.0.52023-02-16T13:50:41.511328ZDownload2730b3fced23063ef4b4a12d0ff2a697d26dbf7dc83e36790a816abea0680b85
2.1.02023-02-16T15:27:45.904325ZDownload070d8fcc12d599497b635f82db6a07f0ba0b756d48126a826b50b0c558745007
2.2.02023-02-17T01:26:39.200947ZDownload06866290206d196064fd61df4c7aea1ffe9a4e7c4ccaa8fcded42dd41948005d
2.2.12023-05-15T11:35:21.494470ZDownload302df7ff2b6a96960e9e447b42e1f238fa30525b92f7ddb47432f8406cd51401
2.2.22023-06-01T13:43:33.061309ZDownload1722b2dcc462b4b2f3ee7d188dad008b6eb4c40bbd03a3de451d82c78bba9aad
2.2.32025-11-15T22:37:58.300488ZDownloadba9dde5f7c6d8ec7ed856bef0eb01c425881acae7748f77dac4d5cc46f2edf04
2.3.02025-11-16T15:29:41.473289ZDownload6780711d2a6de16fa28badff48ed8a0fc6b79a09384eda6eacd2e792fb79530a
2.3.12025-11-16T19:32:10.094594ZDownloadf63cbc48103236abf48e345e07a03ce5757ea86285ed313a6a032596ed9301e2

Dependencies

collection "^1.15.0-nullsafety.4"

crypto "^3.0.0-nullsafety.0"

meta "^1.3.0-nullsafety.6"

typed_data "^1.3.0-nullsafety.5"

zone_local "^0.2.0-nullsafety.0"