Skip to content

Commit 6a91984

Browse files
authored
Merge pull request #98 from thefrontside/tm/switch-all-to-npm
Switch all we can to npm and Node compatible APIs
2 parents e1da8b8 + 67cdad3 commit 6a91984

File tree

15 files changed

+99
-107
lines changed

15 files changed

+99
-107
lines changed

fx/request.test.ts

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,29 @@ import { beforeEach, describe, it } from "@effectionx/bdd";
22
import { expect } from "@std/expect";
33

44
import { json, request } from "./request.ts";
5-
import { ensure, until } from "effection";
5+
import { call, ensure, withResolvers } from "effection";
6+
import { createServer } from "node:http";
67

78
// Ensure to run tests with --allow-net permission
89
describe("request() and json()", () => {
910
let url: string;
1011
beforeEach(function* () {
11-
let server = Deno.serve(
12-
() =>
13-
new Response(JSON.stringify({ id: 1, title: "do things" }), {
14-
headers: new Headers({
15-
"Content-Type": "application/json",
16-
}),
17-
}),
18-
);
12+
let server = createServer((_req, res) => {
13+
res.writeHead(200, { "Content-Type": "application/json" });
14+
res.end(JSON.stringify({ id: 1, title: "do things" }));
15+
});
16+
17+
const ready = withResolvers<void>();
18+
server.listen(0, () => ready.resolve());
19+
yield* ready.operation;
1920

20-
url = `http://localhost:${server.addr.port}/todos/1`,
21-
yield* ensure(() => until(server.shutdown()));
21+
const addr = server.address();
22+
const port = typeof addr === "object" && addr ? addr.port : 0;
23+
24+
url = `http://localhost:${port}/todos/1`;
25+
yield* ensure(() =>
26+
call(() => new Promise<void>((resolve) => server.close(() => resolve())))
27+
);
2228
});
2329

2430
it("returns a response that can be parsed with json", function* () {

jsonl-store/context.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import { type Context, createContext, type Operation } from "effection";
22
import { join } from "@std/path";
3+
import process from "node:process";
34
import type { Store } from "./types.ts";
45
import { JSONLStore } from "./jsonl.ts";
56

67
const DEFAULT_STORE: Store = JSONLStore.from({
7-
location: join(import.meta.dirname ?? Deno.cwd(), ".store"),
8+
location: join(import.meta.dirname ?? process.cwd(), ".store"),
89
});
910

1011
export const StoreContext: Context<Store> = createContext<Store>(

jsonl-store/jsonl.test.ts

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,36 @@
11
import { expect } from "@std/expect";
22
import { dirname, join } from "@std/path";
33
import { beforeEach, describe, it } from "@effectionx/bdd";
4-
import { each, stream, until } from "effection";
5-
import { mkdir } from "node:fs";
6-
import { promisify } from "node:util";
4+
import { each, until } from "effection";
5+
import * as fsp from "node:fs/promises";
6+
import { mkdtemp } from "node:fs/promises";
7+
import { tmpdir } from "node:os";
78

89
import { JSONLStore } from "./jsonl.ts";
910
import type { Store } from "./types.ts";
1011

11-
// using promisify there because Deno's ensure doesn't work
12-
// correctly in Node. We should run these tests in Node
13-
// to make sure that it'll work in Node too.
14-
1512
describe("JSONLStore", () => {
1613
let store: Store;
1714
let tmpDir: string;
1815

1916
async function readTmpFile(fileName: string) {
20-
return await Deno.readTextFile(`${tmpDir}/${fileName}`);
17+
return await fsp.readFile(`${tmpDir}/${fileName}`, "utf-8");
2118
}
2219

2320
async function writeTmpFile(fileName: string, data: string) {
24-
await promisify(mkdir)(join(tmpDir, dirname(fileName)), {
21+
await fsp.mkdir(join(tmpDir, dirname(fileName)), {
2522
recursive: true,
2623
});
27-
await Deno.writeTextFile(join(tmpDir, fileName), data);
24+
await fsp.writeFile(join(tmpDir, fileName), data, "utf-8");
2825
}
2926

3027
async function appendTmpFile(fileName: string, data: string) {
3128
const destination = join(tmpDir, fileName);
32-
const file = await Deno.open(destination, { append: true });
33-
await file.write(new TextEncoder().encode(data));
34-
file.close();
29+
await fsp.appendFile(destination, data, "utf-8");
3530
}
3631

3732
beforeEach(function* () {
38-
tmpDir = yield* until(Deno.makeTempDir());
33+
tmpDir = yield* until(mkdtemp(join(tmpdir(), "jsonl-test-")));
3934
store = JSONLStore.from({ location: tmpDir });
4035
});
4136

@@ -69,18 +64,16 @@ describe("JSONLStore", () => {
6964
});
7065
it("clears store when called clear", function* () {
7166
yield* store.clear();
72-
const entries = [];
73-
for (const dirEntry of yield* each(stream(Deno.readDir(tmpDir)))) {
74-
entries.push(dirEntry);
75-
yield* each.next();
76-
}
67+
const entries = yield* until(fsp.readdir(tmpDir));
7768
expect(entries).toHaveLength(0);
7869
});
7970
});
8071

8172
describe("reading content of a file", () => {
8273
beforeEach(function* () {
83-
yield* until(Deno.writeTextFile(join(tmpDir, "test.jsonl"), `1\n2\n3\n`));
74+
yield* until(
75+
fsp.writeFile(join(tmpDir, "test.jsonl"), `1\n2\n3\n`, "utf-8"),
76+
);
8477
});
8578
it("streams multiple items", function* () {
8679
const items: number[] = [];

jsonl-store/jsonl.ts

Lines changed: 19 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,16 @@ import {
1717
import type { Store, StoreConstructorOptions } from "./types.ts";
1818

1919
import fs from "node:fs";
20-
import { promisify } from "node:util";
20+
import * as fsp from "node:fs/promises";
21+
import { Readable } from "node:stream";
2122

2223
function* mkdir(
2324
path: fs.PathLike,
2425
options: fs.MakeDirectoryOptions & {
2526
recursive: true;
2627
},
2728
): Operation<string | undefined> {
28-
return yield* call(() => promisify(fs.mkdir)(path, options));
29+
return yield* call(() => fsp.mkdir(path, options));
2930
}
3031

3132
export class JSONLStore implements Store {
@@ -110,10 +111,11 @@ export class JSONLStore implements Store {
110111
return resource(function* (provide) {
111112
const channel = createChannel<T, void>();
112113

113-
const file = yield* call(() => Deno.open(location, { read: true }));
114+
const fileStream = fs.createReadStream(fromFileUrl(location));
115+
const webStream = Readable.toWeb(fileStream);
114116

115-
const lines = file
116-
.readable
117+
// deno-lint-ignore no-explicit-any
118+
const lines = (webStream as any)
117119
.pipeThrough(new TextDecoderStream())
118120
.pipeThrough(new TextLineStream())
119121
.pipeThrough(new JsonParseStream());
@@ -153,20 +155,13 @@ export class JSONLStore implements Store {
153155

154156
yield* mkdir(dirname(fromFileUrl(location)), { recursive: true });
155157

156-
const file = yield* call(() =>
157-
Deno.open(location, {
158-
create: true,
159-
write: true,
160-
})
158+
yield* call(() =>
159+
fsp.writeFile(
160+
fromFileUrl(location),
161+
`${JSON.stringify(data)}\n`,
162+
{ encoding: "utf-8" },
163+
)
161164
);
162-
163-
try {
164-
yield* call(() =>
165-
file.write(new TextEncoder().encode(`${JSON.stringify(data)}\n`))
166-
);
167-
} finally {
168-
file.close();
169-
}
170165
}
171166

172167
/**
@@ -185,19 +180,13 @@ export class JSONLStore implements Store {
185180
*append(key: string, data: unknown): Operation<void> {
186181
const location = new URL(`./${key}.jsonl`, this.location);
187182

188-
const file = yield* call(() =>
189-
Deno.open(location, {
190-
append: true,
191-
})
183+
yield* call(() =>
184+
fsp.appendFile(
185+
fromFileUrl(location),
186+
`${JSON.stringify(data)}\n`,
187+
{ encoding: "utf-8" },
188+
)
192189
);
193-
194-
try {
195-
yield* call(() =>
196-
file.write(new TextEncoder().encode(`${JSON.stringify(data)}\n`))
197-
);
198-
} finally {
199-
file.close();
200-
}
201190
}
202191

203192
/**

process/deno.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"effection": "npm:effection@^3",
1111
"shellwords": "npm:shellwords@^1.1.1",
1212
"@std/expect": "jsr:@std/[email protected]",
13-
"@effectionx/stream-helpers": "jsr:@effectionx/[email protected]",
13+
"@effectionx/stream-helpers": "npm:@effectionx/[email protected]",
1414
"@effectionx/bdd": "jsr:@effectionx/[email protected]",
1515
"@effectionx/signals": "npm:@effectionx/[email protected]"
1616
}

process/test/daemon.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { type Daemon, daemon } from "../mod.ts";
77
import { captureError, expectMatch, fetchText } from "./helpers.ts";
88
import { lines } from "../src/helpers.ts";
99

10-
const SystemRoot = Deno.env.get("SystemRoot");
10+
const SystemRoot = process.env.SystemRoot;
1111

1212
describe("daemon", () => {
1313
let task: Task<void>;

process/test/exec.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {
1313
import { exec, type Process, type ProcessResult } from "../mod.ts";
1414
import { lines } from "../src/helpers.ts";
1515

16-
const SystemRoot = Deno.env.get("SystemRoot");
16+
const SystemRoot = process.env.SystemRoot;
1717

1818
// Validate SHELL environment variable is set for proper test execution
1919
if (process.platform === "win32" && !process.env.SHELL) {

signals/deno.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@
1212
".": "./mod.ts"
1313
},
1414
"tasks": {
15-
"test": "deno run -A jsr:@effectionx/watch deno test"
15+
"test": "deno run -A npm:@effectionx/watch deno test"
1616
}
1717
}

stream-helpers/deno.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,6 @@
1616
"./test-helpers": "./test-helpers.ts"
1717
},
1818
"tasks": {
19-
"test": "deno run -A jsr:@effectionx/watch deno test"
19+
"test": "deno run -A npm:@effectionx/watch deno test"
2020
}
2121
}

watch/deno.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
"zod": "npm:zod@^3.20.2",
1919
"zod-opts": "npm:[email protected]",
2020
"@effectionx/bdd": "jsr:@effectionx/[email protected]",
21-
"@effectionx/process": "jsr:@effectionx/[email protected]",
22-
"@effectionx/stream-helpers": "jsr:@effectionx/[email protected]",
21+
"@effectionx/process": "npm:@effectionx/[email protected]",
22+
"@effectionx/stream-helpers": "npm:@effectionx/[email protected]",
2323
"@effectionx/signals": "npm:@effectionx/[email protected]"
2424
},
2525
"lint": {

0 commit comments

Comments
 (0)