Skip to content

feat: support container detection #141

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export default defineBuildConfig({
minify: true,
},
},
entries: ["src/index"],
entries: ["src/index", "src/containers/index", "src/wsl/index"],
hooks: {
"rollup:options"(ctx, rollupConfig) {
(rollupConfig.plugins as Plugin[]).push({
Expand Down
6 changes: 3 additions & 3 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import unjs from "eslint-config-unjs";
export default unjs({
ignores: [],
rules: {
"unicorn/no-null": 0,
"unicorn/prevent-abbreviations": 0
},
"unicorn/no-null": 0,
"unicorn/prevent-abbreviations": 0
},
});
6 changes: 6 additions & 0 deletions playground/bun.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import * as stdEnv from "../src";
import * as stdWsl from "../src/wsl";
import * as stdContainers from "../src/containers";

console.log({
...stdEnv,
process: "-",
});

console.log(stdWsl);

console.log(stdContainers);
6 changes: 6 additions & 0 deletions playground/deno.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
// @ts-nocheck

import * as stdEnv from "../dist/index.mjs";
import * as stdEnvWsl from "../dist/wsl/index.mjs";
import * as stdEnvContainers from "../dist/containers/index.mjs";

Deno.env.set("FOOBAR", "baz");

console.log(stdEnv, stdEnv.process.env.FOOBAR);

console.log(stdEnvWsl);

console.log(stdEnvContainers);
6 changes: 6 additions & 0 deletions playground/node.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import * as stdEnv from "../src";
import * as stdWsl from "../src/wsl";
import * as stdContainers from "../src/containers";

console.log({
...stdEnv,
process: "-",
});

console.log(stdWsl);

console.log(stdContainers);
22 changes: 22 additions & 0 deletions src/containers/container.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { statSync } from "node:fs";
import { isDocker } from "./docker";

function hasContainerEnv() {
try {
statSync("/run/.containerenv");
return true;
} catch {
return false;
}
}

let isContainerCached: boolean;

function _isContainer() {
if (isContainerCached === undefined) {
isContainerCached = hasContainerEnv();
}
return isContainerCached;
}

export const isContainer = _isContainer() || isDocker;
29 changes: 29 additions & 0 deletions src/containers/docker.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { readFileSync, statSync } from "node:fs";

let isDockerCached: boolean;

function _isDocker() {
if (isDockerCached === undefined) {
isDockerCached = hasDockerEnv() || hasDockerCGroup();
}
return isDockerCached;
}

function hasDockerEnv() {
try {
statSync("/.dockerenv");
return true;
} catch {
return false;
}
}

function hasDockerCGroup() {
try {
return readFileSync("/proc/self/cgroup", "utf8").includes("docker");
} catch {
return false;
}
}

export const isDocker = _isDocker();
2 changes: 2 additions & 0 deletions src/containers/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from "./docker";
export * from "./container";
35 changes: 35 additions & 0 deletions src/wsl/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { readFileSync } from "node:fs";
import { release } from "node:os";
import { isDocker } from "../containers";

let isWSLCached: boolean;

function _isWsl() {
if (isWSLCached === undefined) {
isWSLCached = hasUnameOrProcVersion();
}
return isWSLCached;
}

function hasUnameOrProcVersion() {
if (globalThis.process?.platform !== "linux") {
return false;
}
if (release().toLowerCase().includes("microsoft")) {
if (isDocker) {
return false;
}
return true;
}
try {
return readFileSync("/proc/version", "utf8")
.toLowerCase()
.includes("microsoft")
? !isDocker
: false;
} catch {
return false;
}
}

export const isWsl = _isWsl();
20 changes: 20 additions & 0 deletions test/containers.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { expect, it, describe } from "vitest";
import * as stdEnv from "../src/containers";

describe("std-env containers", () => {
it("has expected exports", () => {
expect(Object.keys(stdEnv)).toMatchInlineSnapshot(`
[
"isDocker",
"isContainer",
]
`);
});

it("defaults", () => {
expect(stdEnv).toMatchObject({
isDocker: expect.any(Boolean),
isContainer: expect.any(Boolean),
});
});
});
18 changes: 18 additions & 0 deletions test/wsl.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { expect, it, describe } from "vitest";
import * as stdEnv from "../src/wsl";

describe("std-env wsl", () => {
it("has expected exports", () => {
expect(Object.keys(stdEnv)).toMatchInlineSnapshot(`
[
"isWsl",
]
`);
});

it("defaults", () => {
expect(stdEnv).toMatchObject({
isWsl: expect.any(Boolean),
});
});
});