1 | /**
|
2 | * Converts string from unpack method into a project object. Note: this method
|
3 | * will be expanded greatly in the future in order to support the Scratch 1.4
|
4 | * file format. For now, this is nothing but an (awkward) async wrapper around
|
5 | * the `JSON.parse` function.
|
6 | * @param {string} input Stringified JSON object
|
7 | * @param {Function} callback Returns error or parsed JSON object
|
8 | * @return {void}
|
9 | */
|
10 | module.exports = function (input, callback) {
|
11 | var result;
|
12 | try {
|
13 | // The input is a JSON string, which may contain control characters
|
14 | // that should be removed. See LLK/scratch-vm#1077
|
15 | // So far we've only encountered the backspace control character,
|
16 | // so remove that specific one before continuing.
|
17 | // SB2 JSONs and SB3 JSONs have different versions of the
|
18 | // character serialized (e.g. \u0008 and \b), strip out both versions
|
19 | result = JSON.parse(input.replace(
|
20 | /(\\+)(b|u0008)/g,
|
21 | (match, backslash, code) => {
|
22 | // If the number is odd, there is an actual backspace.
|
23 | if (backslash.length % 2) {
|
24 | // The match contains an actual backspace, instead of backslashes followed by b.
|
25 | // Remove backspace and keep backslashes that are not part of
|
26 | // the control character representation.
|
27 | return match.replace('\\' + code, '');
|
28 | }
|
29 | // They are just backslashes followed by b or u0008. (e.g. "\\b")
|
30 | // Don't replace in this case. (LLK/scratch-parser#56)
|
31 | return match;
|
32 | }
|
33 | ));
|
34 | } catch (e) {
|
35 | return callback(e.toString());
|
36 | }
|
37 | return callback(null, result);
|
38 | };
|