Skip to content

Instantly share code, notes, and snippets.

@sigmaSd
Created August 13, 2022 12:51
Show Gist options
  • Save sigmaSd/494404c8fff6a1f5a361554f5600e0f3 to your computer and use it in GitHub Desktop.
Save sigmaSd/494404c8fff6a1f5a361554f5600e0f3 to your computer and use it in GitHub Desktop.
Get deno.json that was used in this run by deno
export const getDenoJson = async () => {
const parseMaps = (maps: string) => {
const data = [];
for (const line of maps.split("\n")) {
if (!line) {
continue;
}
const [adddrRange, flags, , , , name] = line.split(/ +/);
data.push({
name: name !== "" ? name : undefined,
startAddr: parseInt(adddrRange.split("-")[0], 16),
endAddr: parseInt(adddrRange.split("-")[1], 16),
flags: {
r: flags.includes("r"),
w: flags.includes("w"),
},
});
}
return data;
};
const maps = Deno.readTextFileSync("/proc/self/maps");
const mem = Deno.openSync("/proc/self/mem");
const heap = parseMaps(maps).find((d) => d.name == "[heap]");
Deno.seekSync(mem.rid, heap!.startAddr, Deno.SeekMode.Start);
class Finder extends WritableStream<string> {
sink: Sink;
constructor(start: string, end: string) {
const sink = new Sink(start, end);
super(sink);
this.sink = sink;
}
}
/// use global variable because the class is destroyed when an exception is rose
let target: string | undefined;
class Sink implements UnderlyingSink<string> {
constructor(public startS: string, public endS: string) {
}
write(chunk: string) {
if (!target && chunk.includes(this.endS)) {
const startIndex = chunk.indexOf(this.startS);
const endIndex = startIndex +
chunk.slice(startIndex).indexOf(this.endS) +
this.endS.length;
const result = chunk.slice(startIndex, endIndex);
if (result) {
target = result;
}
}
}
}
const finder = new Finder("file:///", "deno.json");
const stringMem = mem.readable.pipeThrough(new TextDecoderStream()).pipeTo(
finder,
);
try {
await stringMem;
} catch { /*ignore*/ }
return target;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment