Skip to content

Commit 8aa49d9

Browse files
author
Dijana Pavlovic
authored
Avoid importing node module in browser (#1079)
Revert back to checking for global `Buffer`. Fix the issue with missing `typeof` in the original check. For Deno, include the Node API polyfill.)
1 parent 249ad1b commit 8aa49d9

File tree

6 files changed

+45
-23
lines changed

6 files changed

+45
-23
lines changed

deno.lock

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/driver/buildDeno.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ await run({
2323
imports: ["process"],
2424
from: "src/globals.deno.ts",
2525
},
26+
{
27+
imports: ["Buffer"],
28+
from: "src/globals.deno.ts",
29+
},
2630
],
2731
}).then(async () =>
2832
run({
@@ -61,6 +65,7 @@ await run({
6165
{
6266
imports: [
6367
"process",
68+
"Buffer",
6469
"test",
6570
"expect",
6671
"jest",

packages/driver/src/adapter.deno.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,8 @@ import fs from "node:fs/promises";
77
import util from "node:util";
88
import { isIP as _isIP } from "node:net";
99
import { EventEmitter } from "node:events";
10-
import { Buffer } from "node:buffer";
1110

12-
export { path, process, util, fs, Buffer };
11+
export { path, process, util, fs };
1312

1413
export async function readFileUtf8(...pathParts: string[]): Promise<string> {
1514
return await Deno.readTextFile(path.join(...pathParts));

packages/driver/src/adapter.node.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,12 @@ import * as net from "net";
44
import * as os from "os";
55
import * as path from "path";
66
import * as tls from "tls";
7-
import { Buffer } from "node:buffer";
87

98
import process from "process";
109
import * as readline from "readline";
1110
import { Writable } from "stream";
1211

13-
export { path, net, fs, tls, process, Buffer };
12+
export { path, net, fs, tls, process };
1413

1514
export async function readFileUtf8(...pathParts: string[]): Promise<string> {
1615
return await fs.readFile(path.join(...pathParts), { encoding: "utf8" });

packages/driver/src/globals.deno.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import type { MatchResult } from "https://deno.land/x/expect/matchers.ts";
1212
import { bold, green, red } from "https://deno.land/std@0.177.0/fmt/colors.ts";
1313

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

1617
const ACTUAL = red(bold("actual"));
1718
const EXPECTED = green(bold("expected"));

packages/driver/src/primitives/buffer.ts

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,28 +19,46 @@
1919
import type char from "./chars";
2020
import * as chars from "./chars";
2121
import { LegacyHeaderCodes } from "../ifaces";
22-
import { Buffer } from "../adapter.node";
23-
24-
/* WriteBuffer over-allocation */
25-
const BUFFER_INC_SIZE = 4096;
26-
27-
const EMPTY_BUFFER = new Uint8Array(0);
2822

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

32-
const decodeB64 = (b64: string): Uint8Array => {
33-
return Buffer.from(b64, "base64");
34-
};
35-
const encodeB64 = (data: Uint8Array): string => {
36-
const buf = Buffer.isBuffer(data)
37-
? data
38-
: Buffer.from(data.buffer, data.byteOffset, data.byteLength);
39-
return buf.toString("base64");
40-
};
26+
let decodeB64: (_: string) => Uint8Array;
27+
let encodeB64: (_: Uint8Array) => string;
28+
29+
if (typeof Buffer === "function") {
30+
decodeB64 = (b64: string): Uint8Array => {
31+
return Buffer.from(b64, "base64");
32+
};
33+
encodeB64 = (data: Uint8Array): string => {
34+
const buf = !Buffer.isBuffer(data)
35+
? Buffer.from(data.buffer, data.byteOffset, data.byteLength)
36+
: data;
37+
return buf.toString("base64");
38+
};
39+
} else {
40+
decodeB64 = (b64: string): Uint8Array => {
41+
const binaryString = atob(b64);
42+
const size = binaryString.length;
43+
const bytes = new Uint8Array(size);
44+
for (let i = 0; i < size; i++) {
45+
bytes[i] = binaryString.charCodeAt(i);
46+
}
47+
return bytes;
48+
};
49+
encodeB64 = (data: Uint8Array): string => {
50+
const binaryString = String.fromCharCode(...data);
51+
return btoa(binaryString);
52+
};
53+
}
4154

4255
export { decodeB64, encodeB64 };
4356

57+
/* WriteBuffer over-allocation */
58+
const BUFFER_INC_SIZE = 4096;
59+
60+
const EMPTY_BUFFER = new Uint8Array(0);
61+
4462
export class BufferError extends Error {}
4563

4664
export class WriteBuffer {

0 commit comments

Comments
 (0)