Skip to content

Commit

Permalink
fix leaking
Browse files Browse the repository at this point in the history
Signed-off-by: 迷渡 <[email protected]>
  • Loading branch information
justjavac committed Jun 24, 2022
1 parent 34b425b commit 5043069
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 16 deletions.
41 changes: 25 additions & 16 deletions mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,24 +59,29 @@ export default async function machineId(original = false): Promise<string> {
}

async function exec(cmd: string[]): Promise<string> {
const p = Deno.run({
cmd,
stdout: "piped",
stderr: "piped",
});
let p: Deno.Process;

const { code } = await p.status();
p.close();
try {
p = Deno.run({
cmd,
stdout: "piped",
stderr: "piped",
});

if (code === 0) {
const rawOutput = await p.output();
const outputString = new TextDecoder().decode(rawOutput);
return outputString;
} else {
const rawError = await p.stderrOutput();
const errorString = new TextDecoder().decode(rawError);
p.stderr.close();
throw new Error(errorString);
const { code } = await p.status();

if (code === 0) {
const rawOutput = await p.output();
const outputString = new TextDecoder().decode(rawOutput);
return outputString;
} else {
const rawError = await p.stderrOutput();
const errorString = new TextDecoder().decode(rawError);
throw new Error(errorString);
}
} finally {
p!.stderr?.close();
p!.close();
}
}

Expand All @@ -87,3 +92,7 @@ async function hash(guid: string): Promise<string> {
.map((b) => b.toString(16).padStart(2, "0"))
.join("");
}

if (import.meta.main) {
console.log(await machineId());
}
12 changes: 12 additions & 0 deletions test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { assertNotEquals } from "https://deno.land/[email protected]/testing/asserts.ts";
import machineId from "./mod.ts";

const { test } = Deno;

test({
name: "machineId",
fn: async () => {
const id = await machineId();
assertNotEquals(id, "");
},
});

0 comments on commit 5043069

Please sign in to comment.