prepack
Version:
Execute a JS bundle, serialize global state and side effects to a snapshot that can be quickly restored.
79 lines (66 loc) • 2.72 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _invariant = _interopRequireDefault(require("../invariant.js"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
/**
* Copyright (c) 2017-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
// JSON.stringify is not the right choice when writing out giant objects
// to disk. This is an alternative that produces a stream of tokens incrementally
// instead of building a giant in-memory representation first.
// The exported function returns a function that, when called repeatedly,
// provides all the strings that when concatenated together produce the
// result JSON.stringified would have produced on the data.
// After all strings have been provided, the final answer will be undefined.
var _default = data => {
// $FlowFixMe: "symbol" not yet supported by Flow
let isLegal = x => x !== undefined && typeof x !== "function" && typeof x !== "symbol";
(0, _invariant.default)(isLegal(data));
let pushData = (stack, x) => stack.push(typeof x === "object" && x !== null ? x : JSON.stringify(x));
let stack = [];
pushData(stack, data);
let visited = new Set();
return () => {
while (stack.length > 0) {
data = stack.pop();
if (typeof data === "string") return data;
(0, _invariant.default)(typeof data === "object" && data !== null);
if (visited.has(data)) throw new TypeError("Converting circular structure to JSON");
visited.add(data);
if (Array.isArray(data)) {
stack.push("]");
for (let i = data.length - 1; i >= 0; i--) {
let value = data[i];
pushData(stack, isLegal(value) ? value : null);
if (i > 0) stack.push(",");
}
stack.push("[");
} else {
stack.push("}");
let reversedStack = [];
for (let key in data) {
// $FlowFixMe: "symbol" not yet supported by Flow
if (typeof key === "symbol") continue;
let value = data[key];
if (!isLegal(value)) continue;
if (reversedStack.length > 0) reversedStack.push(",");
reversedStack.push(JSON.stringify(key));
reversedStack.push(":");
pushData(reversedStack, value);
}
stack.push(...reversedStack.reverse());
stack.push("{");
}
}
};
};
exports.default = _default;
//# sourceMappingURL=JSONTokenizer.js.map