diff options
author | Luca Casonato <hello@lcas.dev> | 2021-11-22 16:53:58 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-11-22 16:53:58 +0100 |
commit | 3cc724c9bae94c2f79dd4a902782a66f688a1e61 (patch) | |
tree | 2f3feda1880c499c989aaf9e017ea4602ea60ea1 | |
parent | 429c773a2e729a56a551a0b645fd5cbf7ef0fe99 (diff) |
fix(runtime): support reading /proc using readFile (#12839)
-rw-r--r-- | cli/tests/unit/read_file_test.ts | 13 | ||||
-rw-r--r-- | cli/tests/unit/read_text_file_test.ts | 21 | ||||
-rw-r--r-- | runtime/js/40_read_file.js | 15 |
3 files changed, 37 insertions, 12 deletions
diff --git a/cli/tests/unit/read_file_test.ts b/cli/tests/unit/read_file_test.ts index 9b8f5e6df..90a6abc2c 100644 --- a/cli/tests/unit/read_file_test.ts +++ b/cli/tests/unit/read_file_test.ts @@ -105,15 +105,10 @@ unitTest( ); unitTest( - { permissions: { read: true } }, - async function readTextileWithAbortSignal() { - const ac = new AbortController(); - queueMicrotask(() => ac.abort()); - await assertRejects(async () => { - await Deno.readTextFile("cli/tests/testdata/fixture.json", { - signal: ac.signal, - }); - }); + { permissions: { read: true }, ignore: Deno.build.os !== "linux" }, + async function readFileProcFs() { + const data = await Deno.readFile("/proc/self/stat"); + assert(data.byteLength > 0); }, ); diff --git a/cli/tests/unit/read_text_file_test.ts b/cli/tests/unit/read_text_file_test.ts index 8a52831e0..1447fd028 100644 --- a/cli/tests/unit/read_text_file_test.ts +++ b/cli/tests/unit/read_text_file_test.ts @@ -83,3 +83,24 @@ unitTest( assertEquals(resourcesBefore, Deno.resources()); }, ); + +unitTest( + { permissions: { read: true } }, + async function readTextFileWithAbortSignal() { + const ac = new AbortController(); + queueMicrotask(() => ac.abort()); + await assertRejects(async () => { + await Deno.readFile("cli/tests/testdata/fixture.json", { + signal: ac.signal, + }); + }); + }, +); + +unitTest( + { permissions: { read: true }, ignore: Deno.build.os !== "linux" }, + async function readTextFileProcFs() { + const data = await Deno.readTextFile("/proc/self/stat"); + assert(data.length > 0); + }, +); diff --git a/runtime/js/40_read_file.js b/runtime/js/40_read_file.js index 53ac9b08a..5e90b523a 100644 --- a/runtime/js/40_read_file.js +++ b/runtime/js/40_read_file.js @@ -4,13 +4,18 @@ ((window) => { const core = window.Deno.core; const { open, openSync } = window.__bootstrap.files; - const { readAllSyncSized, readAllInnerSized } = window.__bootstrap.io; + const { readAllSync, readAll, readAllSyncSized, readAllInnerSized } = + window.__bootstrap.io; function readFileSync(path) { const file = openSync(path); try { const { size } = file.statSync(); - return readAllSyncSized(file, size); + if (size === 0) { + return readAllSync(file); + } else { + return readAllSyncSized(file, size); + } } finally { file.close(); } @@ -20,7 +25,11 @@ const file = await open(path); try { const { size } = await file.stat(); - return await readAllInnerSized(file, size, options); + if (size === 0) { + return await readAll(file); + } else { + return await readAllInnerSized(file, size, options); + } } finally { file.close(); } |