toolkit/devtools/server/actors/pretty-print-worker.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/devtools/server/actors/pretty-print-worker.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,53 @@
     1.4 +/* -*- Mode: javascript; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2; js-indent-level: 2; -*- */
     1.5 +/* vim: set ft=javascript ts=2 et sw=2 tw=80: */
     1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.9 +
    1.10 +/**
    1.11 + * This file is meant to be loaded as a ChromeWorker. It accepts messages which
    1.12 + * have data of the form:
    1.13 + *
    1.14 + *     { id, url, indent, source }
    1.15 + *
    1.16 + * Where `id` is a unique ID to identify this request, `url` is the url of the
    1.17 + * source being pretty printed, `indent` is the number of spaces to indent the
    1.18 + * code by, and `source` is the source text.
    1.19 + *
    1.20 + * On success, the worker responds with a message of the form:
    1.21 + *
    1.22 + *     { id, code, mappings }
    1.23 + *
    1.24 + * Where `id` is the same unique ID from the request, `code` is the pretty
    1.25 + * printed source text, and `mappings` is an array or source mappings from the
    1.26 + * pretty printed code back to the ugly source text.
    1.27 + *
    1.28 + * In the case of an error, the worker responds with a message of the form:
    1.29 + *
    1.30 + *     { id, error }
    1.31 + */
    1.32 +
    1.33 +importScripts("resource://gre/modules/devtools/acorn/acorn.js");
    1.34 +importScripts("resource://gre/modules/devtools/source-map.js");
    1.35 +importScripts("resource://gre/modules/devtools/pretty-fast.js");
    1.36 +
    1.37 +self.onmessage = (event) => {
    1.38 +  const { data: { id, url, indent, source } } = event;
    1.39 +  try {
    1.40 +    const prettified = prettyFast(source, {
    1.41 +      url: url,
    1.42 +      indent: " ".repeat(indent)
    1.43 +    });
    1.44 +
    1.45 +    self.postMessage({
    1.46 +      id: id,
    1.47 +      code: prettified.code,
    1.48 +      mappings: prettified.map._mappings
    1.49 +    });
    1.50 +  } catch (e) {
    1.51 +    self.postMessage({
    1.52 +      id: id,
    1.53 +      error: e.message + "\n" + e.stack
    1.54 +    });
    1.55 +  }
    1.56 +};

mercurial