source.js (2558B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 "use strict"; 6 7 const { sourceSpec } = require("resource://devtools/shared/specs/source.js"); 8 const { 9 FrontClassWithSpec, 10 registerFront, 11 } = require("resource://devtools/shared/protocol.js"); 12 const { 13 ArrayBufferFront, 14 } = require("resource://devtools/client/fronts/array-buffer.js"); 15 16 /** 17 * A SourceFront provides a way to access the source text of a script. 18 * 19 * @param client DevToolsClient 20 * The DevTools Client instance. 21 * @param form Object 22 * The form sent across the remote debugging protocol. 23 */ 24 class SourceFront extends FrontClassWithSpec(sourceSpec) { 25 constructor(client, form) { 26 super(client); 27 if (form) { 28 this._url = form.url; 29 // this is here for the time being, until the source front is managed 30 // via protocol.js marshalling 31 this.actorID = form.actor; 32 } 33 } 34 35 form(json) { 36 this._url = json.url; 37 } 38 39 get actor() { 40 return this.actorID; 41 } 42 43 get url() { 44 return this._url; 45 } 46 47 // Alias for source.blackbox to avoid changing protocol.js packets 48 blackBox(range) { 49 return this.blackbox(range); 50 } 51 52 // Alias for source.unblackbox to avoid changing protocol.js packets 53 unblackBox() { 54 return this.unblackbox(); 55 } 56 57 /** 58 * Get a Front for either an ArrayBuffer or LongString 59 * for this SourceFront's source. 60 */ 61 async source() { 62 const response = await super.source(); 63 return this._onSourceResponse(response); 64 } 65 66 _onSourceResponse(response) { 67 const { contentType, source } = response; 68 if (source instanceof ArrayBufferFront) { 69 return source.slice(0, source.length).then(function (resp) { 70 if (resp.error) { 71 return resp; 72 } 73 // Keeping str as a string, ArrayBuffer/Uint8Array will not survive 74 // setIn/mergeIn operations. 75 const str = atob(resp.encoded); 76 const newResponse = { 77 source: { 78 binary: str, 79 toString: () => "[wasm]", 80 }, 81 contentType, 82 }; 83 return newResponse; 84 }); 85 } 86 87 return source.substring(0, source.length).then(function (resp) { 88 if (resp.error) { 89 return resp; 90 } 91 92 const newResponse = { 93 source: resp, 94 contentType, 95 }; 96 return newResponse; 97 }); 98 } 99 } 100 101 exports.SourceFront = SourceFront; 102 registerFront(SourceFront);