aboutsummaryrefslogtreecommitdiff
path: root/packages/bun-polyfills/src/utils/misc.ts
diff options
context:
space:
mode:
authorGravatar jhmaster <32803471+jhmaster2000@users.noreply.github.com> 2023-08-10 16:17:39 -0300
committerGravatar GitHub <noreply@github.com> 2023-08-10 12:17:39 -0700
commit40befd8770e90ef2cb74d65bb1e2ff9911e2a2f7 (patch)
tree1860719f46b760aac8a874ea1fb2c24fcce43fb7 /packages/bun-polyfills/src/utils/misc.ts
parent6718950a39614939560ed885b828025e9c45243f (diff)
downloadbun-40befd8770e90ef2cb74d65bb1e2ff9911e2a2f7.tar.gz
bun-40befd8770e90ef2cb74d65bb1e2ff9911e2a2f7.tar.zst
bun-40befd8770e90ef2cb74d65bb1e2ff9911e2a2f7.zip
Sync bun-polyfills branch (#4081)
* bun-polyfills: initial impl. & baseline refactor * move @types/ws dep from root to /test/ * bun-types: remove ReadableStream.forEach method (this does not exist, probably added by mistake) * bun-polyfills: remove extraneous stream utils * bun-polyfills: add types syncing file * bun-polyfills: re-arrange global polyfills * bun-polyfills: fix FileBlob streams types again * bun-polyfills: sync all of @types/node * bun-polyfills: typeguard all current polyfills * bun-polyfills: fix import paths * bun-polyfills: switch to wasm impl. of farmhash * bun-polyfills: support default import of bun obj * bun-polyfills: transpiler placeholder file * bun-polyfills: loaderless import.meta polyfill * bun-polyfills: refactor import.meta polyfill * bun-polyfills: repl entrypoint & todo list index * bun-types: Add null to return type of Bun.which * bun-types: match Bun.sha with Bun.hash.SHA512_256 * bun-polyfills: new "repl" package.json script * bun-polyfills: full refactor of toplevel hashes * bun-polyfills: these are fixed * bun-types: NODE_ENV is optional * bun-polyfills: fix Bun.env types * bun-types+polyfills: fix HeapSnapshot.version type * bun-polyfills: fix some web streams type conflicts * bun-polyfills: update internal FileBlob.slice * bun-polyfills: fix subproc stdin conversions * bun-polyfills: better internal fileblob types * bun-polyfills: try to sync global performance type * bun-polyfills: working zig wasm polyfills setup * bun-polyfills: update scripts * bun-polyfills: fix wasm file location resolution * bun-polyfills: goodbye farmhash (replaced by zig) * bun-polyfills: move all Bun.hash polyfills to zig * bun-polyfills: reimpl. seeding of seeded hashes * bun-polyfills: impl. undocumented murmur32v2 * bun-polyfills: switch zighash from jsdoc to .d.ts * bun-types: partial fix of Hash types * bun-polyfills: documented Hash.murmur32v2 * bun-polyfills: misc updates * bun-polyfills: enable sourcemaps * bun-polyfills: handle empty inputs to hash funcs * bun-types: narrow down hash func types * bun-polyfills: remove unnecessary bigint casts * bun-polyfills: impl. Bun.isMainThread * bun-polyfills: impl. Bun.sleep and fix sleepSync * bun-polyfills: impl. indexOfLine * bun-polyfills: impl. Bun.peek.status * bun-types: fix hashing test --------- Co-authored-by: Jarred Sumner <jarred@jarredsumner.com>
Diffstat (limited to 'packages/bun-polyfills/src/utils/misc.ts')
-rw-r--r--packages/bun-polyfills/src/utils/misc.ts36
1 files changed, 36 insertions, 0 deletions
diff --git a/packages/bun-polyfills/src/utils/misc.ts b/packages/bun-polyfills/src/utils/misc.ts
new file mode 100644
index 000000000..74bb9aa01
--- /dev/null
+++ b/packages/bun-polyfills/src/utils/misc.ts
@@ -0,0 +1,36 @@
+import streams from 'node:stream';
+import type { SpawnOptions, FileBlob } from 'bun';
+
+export const getter = <T>(obj: T, key: string | symbol, get: () => any, enumerable = false, configurable = true): void => {
+ Object.defineProperty(obj, key, { get, configurable, enumerable });
+};
+
+export const setter = <T>(obj: T, key: string | symbol, set: () => any, enumerable = false, configurable = true): void => {
+ Object.defineProperty(obj, key, { set, configurable, enumerable });
+};
+
+export const readonly = <T>(obj: T, key: string | symbol, value: unknown, enumerable = false, configurable = true): void => {
+ Object.defineProperty(obj, key, { value, configurable, enumerable });
+};
+
+export function streamToBuffer(stream: streams.Readable | streams.Duplex): Promise<Buffer> {
+ return new Promise((resolve, reject) => {
+ const buffers: Uint8Array[] = [];
+ stream.on("data", (chunk: Uint8Array) => buffers.push(chunk));
+ stream.on("end", () => resolve(Buffer.concat(buffers)));
+ stream.on("error", (err: Error) => reject(err));
+ });
+}
+
+export function isArrayBufferView(value: any): value is ArrayBufferView {
+ return value !== null && typeof value === 'object' &&
+ value.buffer instanceof ArrayBuffer && typeof value.byteLength === 'number' && typeof value.byteOffset === 'number';
+}
+
+export function isOptions(options: any): options is SpawnOptions.OptionsObject {
+ return options !== null && typeof options === 'object';
+}
+
+export function isFileBlob(blob: any): blob is FileBlob {
+ return blob instanceof Blob && Reflect.get(blob, 'readable') instanceof ReadableStream && typeof Reflect.get(blob, 'writer') === 'function';
+}