universal_io 0.1.1

  • README.md
  • CHANGELOG.md
  • Example
  • Installing
  • Versions
  • 63

Introduction #

A Dart package that supports a subset of dart:io in all platforms, including the browser (where it normally doesn't work).

Typically you just import "package:universal_io/io.dart" (instead of "dart:io"):

  • In the Dart VM and Flutter, the library exports dart:io.
  • In browser: the library exports its own, compatible classes.

Licensed under the MIT License. Much of the source code was adopted from the original 'dart:io' in Dart SDK, which was licensed under a BSD-style license. See LICENSE for all licenses.

Development #

Features #

  • Platform information
    • Information such as operating system, locale, and stdin/stdout. You can override the behavior.
  • HTTP client
    • In browser, the connection to the server is established only after you have committed request body by calling HttpClientRequest method close().
  • File access
    • In browsers, access to all directories and files is denied. You can override the behavior.
  • Sockets

Getting started #

Add dependency #

In pubspec.yaml:

dependencies:
  universal_io: ^0.1.0

Override behavior #

import 'dart:async';

import 'package:universal_io/io.dart';
import 'package:universal_io/io_driver.dart';

void main() async {
  // Set IO driver
  IODriver.zoneLocal.defaultValue = new MyDriver();

  // Do something
  final socket = await Socket.connect("google.com", 80);
  socket.close();
}

class MyDriver extends IODriver {
  @override
  Future<Socket> connectSocket(host, int port,
      {sourceAddress, Duration timeout}) {
    print("Attempting to connect to '$host:$port'");
    return super.connectSocket(host, port);
  }
}

Use Chrome OS driver #

import 'package:universal_io/io.dart';
import 'package:universal_io/io_driver.dart';
import 'package:universal_io_for_chrome_os/io_driver_chrome_os.dart';

void main() async {
  // Use Chrome OS driver for IO
  IODriver.zoneLocal.defaultValue = new ChromeIODriver();
  
  // ...
}

0.1.0 #

  • Added sockets, overrides, and other features.

0.0.1 #

  • Initial release.

example/universal_io.dart

import 'dart:async';

import 'package:universal_io/io.dart';
import 'package:universal_io/io_driver.dart';

void main() async {
  // Set IO driver
  IODriver.zoneLocal.defaultValue = ExampleIODriver();

  // You can now use 'dart:io' APIs
  final socket = await Socket.connect("google.com", 80);
  socket.close();
}

class ExampleIODriver extends IODriver {
  // An example of altering behavior of a socket API
  @override
  Future<Socket> connectSocket(host, int port,
      {sourceAddress, Duration timeout}) {
    print("Attempting to connect to '$host:$port'");
    return super.connectSocket(host, port);
  }
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  universal_io: ^0.1.1

2. Install it

You can install packages from the command line:

with pub:


$ pub get

Alternatively, your editor might support pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:universal_io/io.dart';
import 'package:universal_io/io_driver.dart';
import 'package:universal_io/io_driver_chrome_os.dart';
  
Version Uploaded Documentation Archive
0.1.1 Feb 18, 2019 Go to the documentation of universal_io 0.1.1 Download universal_io 0.1.1 archive
0.1.0 Feb 18, 2019 Go to the documentation of universal_io 0.1.0 Download universal_io 0.1.0 archive
0.0.1 Dec 4, 2018 Go to the documentation of universal_io 0.0.1 Download universal_io 0.0.1 archive
Popularity:
Describes how popular the package is relative to other packages. [more]
34
Health:
Code health derived from static analysis. [more]
86
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
63
Learn more about scoring.

We analyzed this package on Feb 20, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.1.0
  • pana: 0.12.13+1

Platforms

Detected platforms: web

Platform components identified in package: html, io, js.

Health suggestions

Fix lib/src/io/http/http_headers.dart. (-7.24 points)

Analysis of lib/src/io/http/http_headers.dart reported 15 hints, including:

line 89 col 11: Use isEmpty instead of length

line 113 col 3: Avoid return types on setters.

line 139 col 3: Avoid return types on setters.

line 163 col 3: Avoid return types on setters.

line 186 col 3: Avoid return types on setters.

Fix lib/src/io_driver_chrome_os/chrome_io_driver.dart. (-1.49 points)

Analysis of lib/src/io_driver_chrome_os/chrome_io_driver.dart reported 3 hints:

line 29 col 7: Future results in async function bodies must be awaited or marked unawaited using package:pedantic.

line 53 col 7: Future results in async function bodies must be awaited or marked unawaited using package:pedantic.

line 163 col 5: Future results in async function bodies must be awaited or marked unawaited using package:pedantic.

Fix lib/src/io/http/http.dart. (-1 points)

Analysis of lib/src/io/http/http.dart reported 2 hints:

line 193 col 7: Don't explicitly initialize variables to null.

line 1461 col 3: Avoid return types on setters.

Fix additional 8 files with analysis or formatting issues. (-4.50 points)

Additional issues in the following files:

  • lib/src/io/stdio.dart (2 hints)
  • lib/src/io/common.dart (1 hint)
  • lib/src/io/http/crypto.dart (1 hint)
  • lib/src/io/http/http_date.dart (1 hint)
  • lib/src/io/http/http_impl_browser.dart (1 hint)
  • lib/src/io/io_sink.dart (1 hint)
  • lib/src/io_driver/io_driver_browser.dart (1 hint)
  • lib/src/io_driver_chrome_os/chrome/chrome_common.dart (1 hint)

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0 <3.0.0
http ^0.12.0 0.12.0+1
raw ^0.2.0 0.2.0
stream_channel ^1.6.8 1.6.8
typed_data ^1.1.6 1.1.6
Transitive dependencies
charcode 1.1.2
collection 1.14.11
fixnum 0.10.9
http_parser 3.1.3
matcher 0.12.4
meta 1.1.7
path 1.6.2
source_span 1.5.4
stack_trace 1.9.3
string_scanner 1.0.4
term_glyph 1.1.0
Dev dependencies
async ^2.0.8 2.0.8
pedantic ^1.0.0
test ^1.4.0