Skip to content

Commit

Permalink
Avoid importing node module in browser (#1079)
Browse files Browse the repository at this point in the history
Revert back to checking for global `Buffer`. Fix the issue with missing `typeof`
in the original check. For Deno, include the Node API polyfill.)
  • Loading branch information
diksipav authored Aug 22, 2024
1 parent 249ad1b commit 8aa49d9
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 23 deletions.
8 changes: 4 additions & 4 deletions deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions packages/driver/buildDeno.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ await run({
imports: ["process"],
from: "src/globals.deno.ts",
},
{
imports: ["Buffer"],
from: "src/globals.deno.ts",
},
],
}).then(async () =>
run({
Expand Down Expand Up @@ -61,6 +65,7 @@ await run({
{
imports: [
"process",
"Buffer",
"test",
"expect",
"jest",
Expand Down
3 changes: 1 addition & 2 deletions packages/driver/src/adapter.deno.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ import fs from "node:fs/promises";
import util from "node:util";
import { isIP as _isIP } from "node:net";
import { EventEmitter } from "node:events";
import { Buffer } from "node:buffer";

export { path, process, util, fs, Buffer };
export { path, process, util, fs };

export async function readFileUtf8(...pathParts: string[]): Promise<string> {
return await Deno.readTextFile(path.join(...pathParts));
Expand Down
3 changes: 1 addition & 2 deletions packages/driver/src/adapter.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@ import * as net from "net";
import * as os from "os";
import * as path from "path";
import * as tls from "tls";
import { Buffer } from "node:buffer";

import process from "process";
import * as readline from "readline";
import { Writable } from "stream";

export { path, net, fs, tls, process, Buffer };
export { path, net, fs, tls, process };

export async function readFileUtf8(...pathParts: string[]): Promise<string> {
return await fs.readFile(path.join(...pathParts), { encoding: "utf8" });
Expand Down
1 change: 1 addition & 0 deletions packages/driver/src/globals.deno.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import type { MatchResult } from "https://deno.land/x/expect/matchers.ts";
import { bold, green, red } from "https://deno.land/std@0.177.0/fmt/colors.ts";

export { process } from "https://deno.land/std@0.177.0/node/process.ts";
export { Buffer } from "node:buffer";

const ACTUAL = red(bold("actual"));
const EXPECTED = green(bold("expected"));
Expand Down
48 changes: 33 additions & 15 deletions packages/driver/src/primitives/buffer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,46 @@
import type char from "./chars";
import * as chars from "./chars";
import { LegacyHeaderCodes } from "../ifaces";
import { Buffer } from "../adapter.node";

/* WriteBuffer over-allocation */
const BUFFER_INC_SIZE = 4096;

const EMPTY_BUFFER = new Uint8Array(0);

export const utf8Encoder = new TextEncoder();
export const utf8Decoder = new TextDecoder("utf8");

const decodeB64 = (b64: string): Uint8Array => {
return Buffer.from(b64, "base64");
};
const encodeB64 = (data: Uint8Array): string => {
const buf = Buffer.isBuffer(data)
? data
: Buffer.from(data.buffer, data.byteOffset, data.byteLength);
return buf.toString("base64");
};
let decodeB64: (_: string) => Uint8Array;
let encodeB64: (_: Uint8Array) => string;

if (typeof Buffer === "function") {
decodeB64 = (b64: string): Uint8Array => {
return Buffer.from(b64, "base64");
};
encodeB64 = (data: Uint8Array): string => {
const buf = !Buffer.isBuffer(data)
? Buffer.from(data.buffer, data.byteOffset, data.byteLength)
: data;
return buf.toString("base64");
};
} else {
decodeB64 = (b64: string): Uint8Array => {
const binaryString = atob(b64);
const size = binaryString.length;
const bytes = new Uint8Array(size);
for (let i = 0; i < size; i++) {
bytes[i] = binaryString.charCodeAt(i);
}
return bytes;
};
encodeB64 = (data: Uint8Array): string => {
const binaryString = String.fromCharCode(...data);
return btoa(binaryString);
};
}

export { decodeB64, encodeB64 };

/* WriteBuffer over-allocation */
const BUFFER_INC_SIZE = 4096;

const EMPTY_BUFFER = new Uint8Array(0);

export class BufferError extends Error {}

export class WriteBuffer {
Expand Down

0 comments on commit 8aa49d9

Please sign in to comment.