lambda-live-debugger
Version:
Debug Lambda functions locally like it is running in the cloud
47 lines (46 loc) • 1.57 kB
JavaScript
import { NumericValue } from "@smithy/core/serde";
const NUMERIC_CONTROL_CHAR = String.fromCharCode(925);
export class JsonReplacer {
values = new Map();
counter = 0;
stage = 0;
createReplacer() {
if (this.stage === 1) {
throw new Error("@aws-sdk/core/protocols - JsonReplacer already created.");
}
if (this.stage === 2) {
throw new Error("@aws-sdk/core/protocols - JsonReplacer exhausted.");
}
this.stage = 1;
return (key, value) => {
if (value instanceof NumericValue) {
const v = `${NUMERIC_CONTROL_CHAR + "nv" + this.counter++}_` + value.string;
this.values.set(`"${v}"`, value.string);
return v;
}
if (typeof value === "bigint") {
const s = value.toString();
const v = `${NUMERIC_CONTROL_CHAR + "b" + this.counter++}_` + s;
this.values.set(`"${v}"`, s);
return v;
}
return value;
};
}
replaceInJson(json) {
if (this.stage === 0) {
throw new Error("@aws-sdk/core/protocols - JsonReplacer not created yet.");
}
if (this.stage === 2) {
throw new Error("@aws-sdk/core/protocols - JsonReplacer exhausted.");
}
this.stage = 2;
if (this.counter === 0) {
return json;
}
for (const [key, value] of this.values) {
json = json.replace(key, value);
}
return json;
}
}