Skip to content

Fix (and ignore one!) type errors #8

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

Merged
merged 10 commits into from
Mar 22, 2023
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"deno.enable": true
}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import {
prepareVirtualFile,
} from "https://deno.land/x/mock_file@$VERSION/mod.ts";

import { serve } from "https://deno.land/std@0.144.0/http/mod.ts";
import { serve } from "https://deno.land/std@0.180.0/http/mod.ts";
import { DB } from "https://deno.land/x/[email protected]/mod.ts";

// Prepare the file in memory before opening it.
Expand Down
3 changes: 2 additions & 1 deletion deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
"test": "deno test --doc --allow-read=. --allow-write=./tests/",
"check": "deno check ./mod.ts"
},
"importMap": "./import-map.json"
"importMap": "./import-map.json",
"lock": false
}
2 changes: 1 addition & 1 deletion example/main.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { serve } from "https://deno.land/std@0.157.0/http/mod.ts";
import { serve } from "https://deno.land/std@0.180.0/http/mod.ts";
import { DB } from "https://deno.land/x/[email protected]/mod.ts";

import { prepareLocalFile, prepareVirtualFile } from "../mod.ts";
Expand Down
11 changes: 7 additions & 4 deletions src/memory_file.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
import { assert } from "https://deno.land/std@0.157.0/testing/asserts.ts";
import { copy } from "https://deno.land/std@0.157.0/bytes/mod.ts";
import { assert } from "https://deno.land/std@0.180.0/testing/asserts.ts";
import { copy } from "https://deno.land/std@0.180.0/bytes/mod.ts";
import {
fromFileUrl,
resolve,
} from "https://deno.land/std@0.157.0/path/mod.ts";
} from "https://deno.land/std@0.180.0/path/mod.ts";

const defaultFileInfo = {
atime: null,
birthtime: null,
blksize: null,
blocks: null,
dev: null,
// TODO: https://github.com/denoland/deno/pull/18073
// fileInfo.dev is non-nullable.
// is this value collect?
dev: 0,
gid: null,
ino: null,
isDirectory: false,
Expand Down
6 changes: 3 additions & 3 deletions src/memory_file_test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import {
assertEquals,
assertThrows,
} from "https://deno.land/std@0.157.0/testing/asserts.ts";
} from "https://deno.land/std@0.180.0/testing/asserts.ts";
import {
readAll,
writeAll,
} from "https://deno.land/std@0.157.0/streams/mod.ts";
} from "https://deno.land/std@0.180.0/streams/mod.ts";
import { InMemoryFsFile } from "./memory_file.ts";

const targetFile = new URL("../tests/tmp.txt", import.meta.url);
Expand Down Expand Up @@ -82,7 +82,7 @@ async function assertsFileState<T>(
f1: InMemoryFsFile,
f2: Deno.FsFile,
func: (arg: Deno.FsFile) => T | Promise<T>,
expect?: T,
expect?: Awaited<T>,
) {
await assertFileContent(f1);
assertEquals(offset(f1), offset(f2));
Expand Down
33 changes: 21 additions & 12 deletions src/polyfill.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export const createDenoPolyfill = createDenoPolyfillFunc({
seek: (originalFunc, { ridToFile }) => (rid, ...args) => {
const file = ridToFile(rid);
if (file) {
return file.seek(...args);
return file.seek(Number(args[0]), args[1]);
}
return originalFunc(rid, ...args);
},
Expand Down Expand Up @@ -158,11 +158,11 @@ export const createDenoPolyfill = createDenoPolyfillFunc({
}
return originalFunc(path);
},
writeFile: (originalFunc, { pathToFile }) => (path, data, options) => {
writeFile: (originalFunc, { pathToFile }) => async (path, data, options) => {
const file = pathToFile(pathFromURL(path));
if (file) {
file.buffer = data;
return Promise.resolve();
file.buffer = new Uint8Array(await new Response(data).arrayBuffer());
return;
}
return originalFunc(path, data, options);
},
Expand All @@ -174,14 +174,23 @@ export const createDenoPolyfill = createDenoPolyfillFunc({
}
return originalFunc(path, data, options);
},
writeTextFile: (originalFunc, { pathToFile }) => (path, data, options) => {
const file = pathToFile(pathFromURL(path));
if (file) {
file.buffer = encoder.encode(data);
return Promise.resolve();
}
return originalFunc(path, data, options);
},
writeTextFile:
(originalFunc, { pathToFile }) => async (path, data, options) => {
const file = pathToFile(pathFromURL(path));
if (file) {
if (typeof data === "string") {
file.buffer = encoder.encode(data);
return;
} else {
const u8Stream = data.pipeThrough(new TextEncoderStream());
file.buffer = new Uint8Array(
await new Response(u8Stream).arrayBuffer(),
);
return;
}
}
return originalFunc(path, data, options);
},
writeTextFileSync:
(originalFunc, { pathToFile }) => (path, data, options) => {
const file = pathToFile(pathFromURL(path));
Expand Down
76 changes: 73 additions & 3 deletions src/polyfill_test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { assertEquals } from "https://deno.land/std@0.157.0/testing/asserts.ts";
import { assertEquals } from "https://deno.land/std@0.180.0/testing/asserts.ts";

import { createDenoPolyfill } from "./polyfill.ts";
import { InMemoryFsFile, pathFromURL, VirtualFile } from "./memory_file.ts";
Expand Down Expand Up @@ -202,13 +202,49 @@ Deno.test({
});

Deno.test({
name: "polyfill - writeFile",
name: "polyfill - writeFile w/ Uint8Array",
async fn() {
await DenoPolyfill.writeFile(url, new Uint8Array([0, 1, 2]));
assertEquals<unknown>(defaultBuffer.buffer, new Uint8Array([0, 1, 2]));
},
});

Deno.test({
name: "polyfill - writeFile w/ ReadableStream",
async fn() {
let timeout: number;
await DenoPolyfill.writeFile(
url,
new ReadableStream({
start(controller) {
const words = defaultFileContent.split(" ");
return new Promise((resolve, _reject) => {
timeout = setInterval(() => {
const word = words.shift();
if (word) {
controller.enqueue(
new TextEncoder().encode(word + (words.length ? " " : "")),
);
} else {
clearInterval(timeout);
controller.close();
resolve();
}
}, 1);
});
},
cancel() {
clearTimeout(timeout);
},
}),
);
assertEquals<unknown>(
defaultBuffer.buffer,
new TextEncoder().encode(defaultFileContent),
);
},
});

Deno.test({
name: "polyfill - writeFileSync",
fn() {
Expand All @@ -218,7 +254,7 @@ Deno.test({
});

Deno.test({
name: "polyfill - writeTextFile",
name: "polyfill - writeTextFile w/ string",
async fn() {
await DenoPolyfill.writeTextFile(url, defaultFileContent);
assertEquals<unknown>(
Expand All @@ -228,6 +264,40 @@ Deno.test({
},
});

Deno.test({
name: "polyfill - writeTextFile w/ ReadableStream",
async fn() {
let timeout: number;
await DenoPolyfill.writeTextFile(
url,
new ReadableStream({
start(controller) {
const words = defaultFileContent.split(" ");
return new Promise((resolve, _reject) => {
timeout = setInterval(() => {
const word = words.shift();
if (word) {
controller.enqueue(word + (words.length ? " " : ""));
} else {
clearInterval(timeout);
controller.close();
resolve();
}
}, 1);
});
},
cancel() {
clearTimeout(timeout);
},
}),
);
assertEquals<unknown>(
defaultBuffer.buffer,
new TextEncoder().encode(defaultFileContent),
);
},
});

Deno.test({
name: "polyfill - writeTextFileSync",
fn() {
Expand Down