michael@0: /* -*- Mode: javascript; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2; js-indent-level: 2; -*- */ michael@0: /* vim: set ft=javascript ts=2 et sw=2 tw=80: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: /** michael@0: * This file is meant to be loaded as a ChromeWorker. It accepts messages which michael@0: * have data of the form: michael@0: * michael@0: * { id, url, indent, source } michael@0: * michael@0: * Where `id` is a unique ID to identify this request, `url` is the url of the michael@0: * source being pretty printed, `indent` is the number of spaces to indent the michael@0: * code by, and `source` is the source text. michael@0: * michael@0: * On success, the worker responds with a message of the form: michael@0: * michael@0: * { id, code, mappings } michael@0: * michael@0: * Where `id` is the same unique ID from the request, `code` is the pretty michael@0: * printed source text, and `mappings` is an array or source mappings from the michael@0: * pretty printed code back to the ugly source text. michael@0: * michael@0: * In the case of an error, the worker responds with a message of the form: michael@0: * michael@0: * { id, error } michael@0: */ michael@0: michael@0: importScripts("resource://gre/modules/devtools/acorn/acorn.js"); michael@0: importScripts("resource://gre/modules/devtools/source-map.js"); michael@0: importScripts("resource://gre/modules/devtools/pretty-fast.js"); michael@0: michael@0: self.onmessage = (event) => { michael@0: const { data: { id, url, indent, source } } = event; michael@0: try { michael@0: const prettified = prettyFast(source, { michael@0: url: url, michael@0: indent: " ".repeat(indent) michael@0: }); michael@0: michael@0: self.postMessage({ michael@0: id: id, michael@0: code: prettified.code, michael@0: mappings: prettified.map._mappings michael@0: }); michael@0: } catch (e) { michael@0: self.postMessage({ michael@0: id: id, michael@0: error: e.message + "\n" + e.stack michael@0: }); michael@0: } michael@0: };