Compare commits

...

1 Commits

Author SHA1 Message Date
Georgijs Vilums
52e8814246 implement mkfifo on windows 2024-02-07 18:13:01 -08:00
5 changed files with 67 additions and 34 deletions

View File

@@ -1,7 +1,12 @@
// @known-failing-on-windows: 1 failing
import { ArrayBufferSink } from "bun";
import { describe, expect, it } from "bun:test";
import { isWindows } from "harness";
import { mkfifo } from "mkfifo";
import { join } from "path";
import { tmpdir } from "os";
const dir = tmpdir();
describe("FileSink", () => {
const fixtures = [
@@ -43,7 +48,7 @@ describe("FileSink", () => {
] as const;
function getPath(label: string) {
const path = `/tmp/bun-test-${Bun.hash(label).toString(10)}.txt`;
const path = join(dir, `bun-test-${Bun.hash(label).toString(10)}.txt`);
try {
require("fs").unlinkSync(path);
} catch (e) {}
@@ -54,11 +59,11 @@ describe("FileSink", () => {
var decoder = new TextDecoder();
function getFd(label: string) {
const path = `/tmp/bun-test-${Bun.hash(label).toString(10)}.txt`;
try {
require("fs").unlinkSync(path);
} catch (e) {}
mkfifo(path, 0o666);
const name = `bun-test-${Bun.hash(label).toString(10)}.txt`;
// try {
// require("fs").unlinkSync(path);
// } catch (e) {}
const path = mkfifo(name, 0o666);
activeFIFO = (async function (stream: ReadableStream<Uint8Array>) {
var chunks: Uint8Array[] = [];
for await (const chunk of stream) {

View File

@@ -6,7 +6,7 @@ import { mkfifo } from "mkfifo";
import { tmpdir } from "os";
import { gzipSync } from "zlib";
import { join } from "path";
import { gc, withoutAggressiveGC, gcTick } from "harness";
import { gc, withoutAggressiveGC, gcTick, isWindows } from "harness";
import net from "net";
const tmp_dir = mkdtempSync(join(realpathSync(tmpdir()), "fetch.test"));
@@ -830,9 +830,8 @@ describe("Bun.file", () => {
return file;
});
it("size is Infinity on a fifo", () => {
const path = join(tmp_dir, "test-fifo");
mkfifo(path);
it.only("size is Infinity on a fifo", () => {
const path = mkfifo("test-fifo");
const { size } = Bun.file(path);
expect(size).toBe(Infinity);
});
@@ -844,7 +843,7 @@ describe("Bun.file", () => {
}
}
describe("bad permissions throws", () => {
describe.skipIf(isWindows)("bad permissions throws", () => {
const path = join(tmp_dir, "my-new-file");
beforeAll(async () => {
await Bun.write(path, "hey");

View File

@@ -1,7 +1,6 @@
import { afterAll, beforeAll, describe, expect, it, test } from "bun:test";
import fs, { chmodSync, unlinkSync } from "fs";
import { gc, withoutAggressiveGC } from "harness";
import { mkfifo } from "mkfifo";
describe("FormData", () => {
it("should be able to append a string", () => {

View File

@@ -420,12 +420,12 @@ it("ReadableStream.prototype.values", async () => {
});
it("Bun.file() read text from pipe", async () => {
try {
unlinkSync("/tmp/fifo");
} catch (e) {}
// try {
// unlinkSync("/tmp/fifo");
// } catch (e) {}
console.log("here");
mkfifo("/tmp/fifo", 0o666);
const path = mkfifo("fifo", 0o666);
// 65k so its less than the max on linux
const large = "HELLO!".repeat((((1024 * 65) / "HELLO!".length) | 0) + 1);
@@ -433,7 +433,7 @@ it("Bun.file() read text from pipe", async () => {
const chunks = [];
const proc = Bun.spawn({
cmd: ["bash", join(import.meta.dir + "/", "bun-streams-test-fifo.sh"), "/tmp/fifo"],
cmd: ["bash", join(import.meta.dir + "/", "bun-streams-test-fifo.sh"), path],
stderr: "inherit",
stdout: null,
stdin: null,
@@ -446,7 +446,7 @@ it("Bun.file() read text from pipe", async () => {
const prom = (async function () {
while (chunks.length === 0) {
var out = Bun.file("/tmp/fifo").stream();
var out = Bun.file(path).stream();
for await (const chunk of out) {
chunks.push(chunk);
}

View File

@@ -1,22 +1,52 @@
import { dlopen, ptr } from "bun:ffi";
import { isWindows } from "harness";
import { join } from "path";
var lazyMkfifo: any;
export function mkfifo(path: string, permissions: number = 0o666): void {
if (!lazyMkfifo) {
const suffix = process.platform === "darwin" ? "dylib" : "so.6";
lazyMkfifo = dlopen(`libc.${suffix}`, {
mkfifo: {
args: ["ptr", "i32"],
returns: "i32",
},
}).symbols.mkfifo;
}
var ffiFunc: any;
export function mkfifo(pipename: string, permissions: number = 0o666): string {
if (isWindows) {
if (!ffiFunc) {
ffiFunc = dlopen("kernel32.dll", {
CreateNamedPipeA: {
args: ["ptr", "i32", "i32", "i32", "i32", "i32", "i32", "ptr"],
returns: "ptr",
},
}).symbols.CreateNamedPipeA;
}
const buf = new Uint8Array(Buffer.byteLength(path) + 1);
new TextEncoder().encodeInto(path, buf);
const rc = lazyMkfifo(ptr(buf), permissions);
const path = `\\\\.\\pipe\\${pipename}`;
const buf = new Uint8Array(Buffer.byteLength(path) + 1);
new TextEncoder().encodeInto(path, buf);
if (rc < 0) {
throw new Error(`mkfifo failed`);
const rc = ffiFunc(ptr(buf), 0x00000003, 0, 255, 1024, 1024, 0, null);
// todo this check probably doesn't quite do the right thing
if (rc === 18446744073709551615) {
throw new Error(`CreateNamedPipeA failed`);
}
return path;
return path;
} else {
if (!ffiFunc) {
const suffix = process.platform === "darwin" ? "dylib" : "so.6";
ffiFunc = dlopen(`libc.${suffix}`, {
mkfifo: {
args: ["ptr", "i32"],
returns: "i32",
},
}).symbols.mkfifo;
}
const path = join("/tmp", pipename);
const buf = new Uint8Array(Buffer.byteLength(path) + 1);
new TextEncoder().encodeInto(path, buf);
const rc = ffiFunc(ptr(buf), permissions);
if (rc < 0) {
throw new Error(`mkfifo failed`);
}
return path;
}
}