Skip to content

Commit

Permalink
Revert buffer changes.
Browse files Browse the repository at this point in the history
  • Loading branch information
dsherret committed Dec 6, 2024
1 parent 6710ce8 commit 581382e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 18 deletions.
11 changes: 4 additions & 7 deletions src/common.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { assertEquals } from "@std/assert";
import { Buffer } from "@std/io/buffer";
import * as path from "@std/path";
import {
delayToIterator,
Expand Down Expand Up @@ -107,13 +108,9 @@ Deno.test("gets file name from url", () => {

Deno.test("gets the executable shebang when it exists", async () => {
function get(input: string) {
const stream = new ReadableStream<Uint8Array>({
start(controller) {
controller.enqueue(new TextEncoder().encode(input));
controller.close();
},
});
return getExecutableShebang(stream);
const data = new TextEncoder().encode(input);
const buffer = new Buffer(data);
return getExecutableShebang(buffer);
}

assertEquals(
Expand Down
25 changes: 14 additions & 11 deletions src/common.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { TextLineStream } from "@std/streams/text-line-stream";
import { BufReader } from "@std/io/buf-reader";
import * as path from "@std/path";
import { logger } from "./console/mod.ts";
import type { Reader } from "./pipes.ts";

interface Symbols {
/** Use this symbol to enable the provided object to be written to in
Expand Down Expand Up @@ -244,7 +245,7 @@ export async function getExecutableShebangFromPath(path: string) {
try {
const file = await Deno.open(path, { read: true });
try {
return await getExecutableShebang(file.readable);
return await getExecutableShebang(file);
} finally {
try {
file.close();
Expand All @@ -265,18 +266,20 @@ export interface ShebangInfo {
command: string;
}

export async function getExecutableShebang(readable: ReadableStream<Uint8Array>): Promise<ShebangInfo | undefined> {
const decoder = new TextDecoder();
export async function getExecutableShebang(reader: Reader): Promise<ShebangInfo | undefined> {
const text = "#!/usr/bin/env ";
const reader = await readable
.pipeThrough(new TextDecoderStream())
.pipeThrough(new TextLineStream())
.getReader();
const { value } = await reader.read();
await reader.cancel();
if (value === undefined || !value.startsWith(text)) {
const buffer = new Uint8Array(text.length);
const bytesReadCount = await reader.read(buffer);
if (bytesReadCount !== text.length || decoder.decode(buffer) !== text) {
return undefined;
}
const result = value?.replace(text, "").trim();
const bufReader = new BufReader(reader);
const line = await bufReader.readLine();
if (line == null) {
return undefined;
}
const result = decoder.decode(line.line).trim();
const dashS = "-S ";
if (result.startsWith(dashS)) {
return {
Expand Down

0 comments on commit 581382e

Please sign in to comment.