Posts

Dart Crypto early access

The Dart project just saw its first crypto libraries land , specifically for SHA1 and SHA256. Also appearing is HMAC support. Learn how to use these very new libraries in this post. This new functionality is very new. You'll need to pull the latest from the bleeding_edge branch, as of 2012-04-30. It's so new, it's not even wired into the dart: library scheme, nor is it in the SDK yet. I expect crypto libs to get the full SDK treatment very soon. Here's an example of how to create a SHA256 hash and convert to a hex string: #import('../dart/lib/crypto/crypto.dart'); // Want this in the crypto lib? Star this bug: http://code.google.com/p/dart/issues/detail?id=2839 String digestToString(List<int> digest) {   var buf = new StringBuffer();   for (var part in digest) {     buf.add("${(part < 16) ? "0" : ""}${part.toRadixString(16).toLowerCase()}");   }   return buf.toString(); } main() {   var sha = new SHA256()...

Dart Server supports Web Sockets

[EDIT: Updated on 2013-12-05] Dart is a structured web programming language that runs on the client and the server. Web sockets are bi-directional data channels for real-time streaming communication between modern web browsers and servers. Thanks to a recent commit , Dart Servers can host Web Socket connections now! You'll need a copy of the Dart SDK as of 2013-03-05 for this to work. Dart already ships with basic HttpServer functionality. The new Web Socket is built on top of HttpServer, so it slides right into an existing Dart server app. The following code snippet is a simple example of a Web Socket echo server running in Dart. import 'dart:io'; void main() { HttpServer.bind('127.0.0.1', port) .then((HttpServer server) { print('listening for connections on $port'); server.listen((HttpRequest request) { if (request.uri.path == '/ws') { WebSocketTransformer.upgrade(request).then((WebSocket w...

4 new changes to the Dart language spec

The Dart team published version 0.08 of the Dart language spec , including 15 changes. I've detailed four of the most exciting changes below, some of which you might have seen as early proposals. Not all of these changes are implemented yet, but they show what direction the language and team is moving. Lazily Initialization for static variables This change was proposed  in February 2012. Previously, static class variables and top level variables had to be compile time constants. This kept initialization costs at startup low, but otherwise was constraining to the developer. With this change, static class variables and top level variables will be initialized at first access (lazily) and no longer need to constant. This is a developer friendly change that keeps initialization costs low. Version 0.08 of the spec now reads "Static variable declarations are initialized lazily. The first time a static variable v is read, it is set to the result of evaluating its initial...

Dart templates now allow nesting

UPDATE: Work on this library has stopped. You probably want to see Web UI , the fully supported modern client-side library for dynamic, data-driven web apps. Just a few days after we see the first hints at a Dart template library, new features and fixes have landed to make Dart templates even more useful. This is Part 2 of our exploration of Dart templates, read Part One for an introduction to Dart templates . Included in this new commit are: fixed bug with whitespace being removed from text nodes added local names for #with and #each added ability to call another template from within a template Let's take these new features for a spin. Local names Using local names, we can loop through a simple List of Strings.  For example, given the following simple script: #import('dart:html'); #source('hello.dart'); main() {   List fruits = ['apples', 'oranges', 'bananas'];   Hello hello = new Hello("Bob",...

JSONP with Dart

A few people have asked how to handle JSONP in Dart. Turns out, this is basically possible, and I'd appreciate feedback on this technique. JSONP is a trick to get around the lack of CORS headers in your favorite API. CORS is the modern way to get around the single origin policy, however even Google still doesn't support CORS on many of their APIs. The web developer community has come up with JSONP as a hack until all browsers and all APIs support CORS. Option 1: If you always deploy to JavaScript This method only works if you are always deploying to JavaScript and are not deploying or testing on Dartium (Chromium with an embedded Dart VM). Also, this feels hacky. Add the JSONP callback to your main page as a small JavaScript method. <script type="text/javascript"> function callbackForJsonpApi(data) {   dartCallback(JSON.stringify(data)); } </script> This simple method converts the data from the server into a big JSON string...

First look at Dart's HTML template library

UPDATE: Work on this library has stopped. You probably want to see Polymer  or Angular . Note: this is very very early access stuff. Feedback most welcome! Dart is built to help developers build modern web apps. With today's commit of an HTML template library, that job just got a little bit easier. I take a first look at this new (and still evolving) template library to see what it does and how it works. This week has been full of great stuff in the Dart community. Just a sample: Lars Bak and Kasper Lund join the JavaScript Jabber podcast, support for adjacent string literals lands in the Dart Editor , and the article I co-authored titled What is Dart? was published by O'Reilly. With this new template library, it looks like there is no slowing down. Intro Modern web apps process data and generate display content on the client . The days of full page refreshes and expensive server round trips are gone. Modern web app frameworks usually ship with some sort of ...

Concatenating string literals in Dart

UPDATE: Dart now supports the + concatenator for string. Dart supports multiple ways to concatenate strings: string interpolation, the + operator, and adjacent string literals. It turns out that  puzzles arise from the using + to concatenate strings, so the Dart team felt compelled to take a fresh approach. Dart already had string interpolation, which allows you to embed a string within a string: // String interpolation in Dart String to = 'Bob'; String msg = "Hello $to";  // Hello Bob Dart also has multi-line Strings, using triple quotes: String htmlTemplate = """ <div>   <p>     Hello $to.   </p> </div>"""; Sometimes, however, you need to deal with very long strings. You can use the + operator to concat long strings, or use adjacent string literals. // This works String longMessage = 'This is what you used to do in Dart. ' +                      'Yo...