pub_server 0.1.5

  • Readme
  • Changelog
  • Example
  • Installing
  • 70

Provides re-usable code for making a Dart package repository server. The package:pub_server/shelf_pubserver.dart library provides a shelf HTTP handler which provides the HTTP API used by the pub client. One can use different backend implementations by implementing the PackageRepository interface of the package:pub_server/repository.dart library.

Example pub repository server #

An experimental pub server based on a file system can be found in example/example.dart. It uses a filesystem-based PackageRepository for storing packages and has a read-only fallback to the real pub.dartlang.org site, if a package is not available locally. This allows one to use all pub.dartlang.org packages and have additional ones, on top of the publicly available packages, available only locally.

It can be run as follows

~ $ git clone https://github.com/dart-lang/pub_server.git
~ $ cd pub_server
~/pub_server $ pub get
...
~/pub_server $ dart example/example.dart -d /tmp/package-db
Listening on http://localhost:8080

To make the pub client use this repository configure your shell via:

    $ export PUB_HOSTED_URL=http://localhost:8080

Using it for uploading new packages to the locally running server or downloading packages locally available or via a fallback to pub.dartlang.org is as easy as:

~/foobar $ export PUB_HOSTED_URL=http://localhost:8080
~/foobar $ pub get
...
~/foobar $ pub publish
Publishing x 0.1.0 to http://localhost:8080:
|-- ...
'-- pubspec.yaml

Looks great! Are you ready to upload your package (y/n)? y
Uploading...
Successfully uploaded package.

The fact that the pub publish command requires you to grant it oauth2 access - which requires a Google account - is due to the fact that the pub publish cannot work without authentication or with another authentication scheme. But the information sent by the pub client is not used for this local server at the moment.

NOTE: This is package is an alpha version and is not recommended for production use.

0.1.5 #

  • Differentiate between client- and server-side upload issues.

0.1.4+2 #

  • Drop support for Dart 1.x.

0.1.4+1 #

  • Support Dart 2 stable releases.

  • Support latest release of package:pub_semver.

0.1.4 #

  • Dart 2 support with dart2_constant.

0.1.3 #

  • PackageRepository.download now has more specific return type: Future<Stream<List<int>>>.

  • Fix incorrect boundary parsing during upload.

  • Update minimum Dart SDK to 1.23.0.

0.1.2 #

  • Add support for generic exceptions raised e.g. due to pubspec.yaml validation failure.

0.1.1+4 #

  • Support the latest version of pkg/shelf.

0.1.1+3 #

  • Support the latest release of pub_semver.

0.1.1+2 #

  • Fixed null comparision.

0.1.1+1 #

  • Updated dependencies.

0.1.1 #

  • Return "400 Bad Request" in case the version number encoded in the URL is not a valid semantic version

0.1.0 #

Initial release.

example/example.dart

// Copyright (c) 2014, the Dart project authors.  Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'dart:io';

import 'package:args/args.dart';
import 'package:logging/logging.dart';
import 'package:shelf/shelf.dart';
import 'package:shelf/shelf_io.dart' as shelf_io;
import 'package:http/http.dart' as http;
import 'package:pub_server/shelf_pubserver.dart';

import 'src/examples/cow_repository.dart';
import 'src/examples/file_repository.dart';
import 'src/examples/http_proxy_repository.dart';

final Uri pubDartLangOrg = Uri.parse('https://pub.dartlang.org');

main(List<String> args) {
  var parser = argsParser();
  var results = parser.parse(args);

  var directory = results['directory'] as String;
  var host = results['host'] as String;
  var port = int.parse(results['port'] as String);
  var standalone = results['standalone'] as bool;

  if (results.rest.length > 0) {
    print('Got unexpected arguments: "${results.rest.join(' ')}".\n\nUsage:\n');
    print(parser.usage);
    exit(1);
  }

  setupLogger();
  runPubServer(directory, host, port, standalone);
}

runPubServer(String baseDir, String host, int port, bool standalone) {
  var client = new http.Client();

  var local = new FileRepository(baseDir);
  var remote = new HttpProxyRepository(client, pubDartLangOrg);
  var cow = new CopyAndWriteRepository(local, remote, standalone);

  var server = new ShelfPubServer(cow);
  print('Listening on http://$host:$port\n'
      '\n'
      'To make the pub client use this repository configure your shell via:\n'
      '\n'
      '    \$ export PUB_HOSTED_URL=http://$host:$port\n'
      '\n');

  return shelf_io.serve(
      const Pipeline()
          .addMiddleware(logRequests())
          .addHandler(server.requestHandler),
      host,
      port);
}

ArgParser argsParser() {
  var parser = new ArgParser();

  parser.addOption('directory',
      abbr: 'd', defaultsTo: 'pub_server-repository-data');

  parser.addOption('host', abbr: 'h', defaultsTo: 'localhost');

  parser.addOption('port', abbr: 'p', defaultsTo: '8080');
  parser.addFlag('standalone', abbr: 's', defaultsTo: false);
  return parser;
}

void setupLogger() {
  Logger.root.onRecord.listen((LogRecord record) {
    var head = '${record.time} ${record.level} ${record.loggerName}';
    var tail = record.stackTrace != null ? '\n${record.stackTrace}' : '';
    print('$head ${record.message} $tail');
  });
}

Use this package as a library

1. Depend on it

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


dependencies:
  pub_server: ^0.1.5

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter pub get

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

3. Import it

Now in your Dart code, you can use:


import 'package:pub_server/repository.dart';
import 'package:pub_server/shelf_pubserver.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
48
Health:
Code health derived from static analysis. [more]
98
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
80
Overall:
Weighted score of the above. [more]
70
Learn more about scoring.

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

  • Dart: 2.6.0
  • pana: 0.12.21

Platforms

Detected platforms: Flutter, web, other

No platform restriction found in libraries.

Health suggestions

Fix lib/shelf_pubserver.dart. (-1.99 points)

Analysis of lib/shelf_pubserver.dart reported 4 hints:

line 245 col 9: Use isEmpty instead of length

line 421 col 53: Use isNotEmpty instead of length

line 477 col 62: Use = to separate a named parameter from its default value.

line 482 col 53: Use = to separate a named parameter from its default value.

Maintenance suggestions

Package is getting outdated. (-19.73 points)

The package was last published 62 weeks ago.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0-dev <3.0.0
dart2_constant ^1.0.1 1.0.2+dart2
http_parser ^3.0.0 3.1.3
logging >=0.9.3 <1.0.0 0.11.3+2
mime >=0.9.3 <0.10.0 0.9.6+3
pub_semver ^1.1.0 1.4.2
shelf >=0.5.6 <0.8.0 0.7.5
yaml ^2.1.2 2.2.0
Transitive dependencies
async 2.4.0
charcode 1.1.2
collection 1.14.12
meta 1.1.8
source_span 1.5.5
stack_trace 1.9.3
stream_channel 2.0.0
string_scanner 1.0.5
term_glyph 1.1.0
typed_data 1.1.6
Dev dependencies
archive >=1.0.0 <3.0.0
args ^1.4.0
http ^0.11.0
path ^1.5.1 1.6.4
test ^1.3.0