Skip to content

Commit

Permalink
feat: improve node polyfills
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed May 5, 2022
1 parent 16338d4 commit 4206317
Show file tree
Hide file tree
Showing 9 changed files with 128 additions and 39 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ Module | Status | Source
[node:dns](https://nodejs.org/api/dns.html) | Mocked | -
[node:domain](https://nodejs.org/api/domain.html) | Mocked | -
[node:events](https://nodejs.org/api/events.html) | Polyfilled | [unenv/node/events](./src/runtime/node/events)
[node:fs/promises](https://nodejs.org/api/fs/promises.html) | Mocked | -
[node:fs](https://nodejs.org/api/fs.html) | Mocked | -
[node:fs/promises](https://nodejs.org/api/fs/promises.html) | Mocked | -
[node:http2](https://nodejs.org/api/http2.html) | Mocked | -
[node:http](https://nodejs.org/api/http.html) | Polyfilled | [unenv/node/http](./src/runtime/node/http)
[node:https](https://nodejs.org/api/https.html) | Mocked | -
Expand All @@ -72,14 +72,19 @@ Module | Status | Source
[node:readline](https://nodejs.org/api/readline.html) | Mocked | -
[node:repl](https://nodejs.org/api/repl.html) | Mocked | -
[node:stream](https://nodejs.org/api/stream.html) | Polyfilled | [unenv/node/stream](./src/runtime/node/stream)
[node:stream/consumers](https://nodejs.org/api/stream.html) | Mocked | [unenv/node/stream/consumers](./src/runtime/node/stream/consumers)
[node:stream/promises](https://nodejs.org/api/stream.html) | Mocked | [unenv/node/stream/promises](./src/runtime/node/stream/promises)
[node:stream/web](https://nodejs.org/api/stream.html) | Native | [unenv/node/stream/web](./src/runtime/node/stream/web)
[node:string_decoder](https://nodejs.org/api/string_decoder.html) | Mocked | -
[node:sys](https://nodejs.org/api/sys.html) | Mocked | -
[node:timers](https://nodejs.org/api/timers.html) | Mocked | -
[node:timers/promises](https://nodejs.org/api/timers.html) | Mocked | -
[node:tls](https://nodejs.org/api/tls.html) | Mocked | -
[node:trace_events](https://nodejs.org/api/trace_events.html) | Mocked | -
[node:tty](https://nodejs.org/api/tty.html) | Mocked | -
[node:url](https://nodejs.org/api/url.html) | Polyfilled | [unenv/node/url](./src/runtime/node/url)
[node:util](https://nodejs.org/api/util.html) | Polyfilled | [unenv/node/util](./src/runtime/node/util)
[node:util/types](https://nodejs.org/api/util.html) | Polyfilled | [unenv/node/util/types](./src/runtime/node/util/types)
[node:v8](https://nodejs.org/api/v8.html) | Mocked | -
[node:vm](https://nodejs.org/api/vm.html) | Mocked | -
[node:wasi](https://nodejs.org/api/wasi.html) | Mocked | -
Expand Down
6 changes: 5 additions & 1 deletion src/presets/nodeless.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,12 @@ const nodeless: Preset = {
'path',
'process',
'stream',
'stream/promises',
'stream/consumers',
'stream/web',
'url',
'util'
'util',
'util/types'
].map(m => [m, `unenv/runtime/node/${m}/index`])),

// npm
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/_internal/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@ export function mergeFns (...functions: Function[]) {
}

export function notImplemented (name: string) {
return () => { throw new Error(`[unenv] ${name} is not implemented yet!`) }
return () : any => { throw new Error(`[unenv] ${name} is not implemented yet!`) }
}
5 changes: 2 additions & 3 deletions src/runtime/node/buffer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,13 @@ export const atob = globalThis.atob
export const kStringMaxLength = 0 // TODO
export const constants = { MAX_LENGTH: kMaxLength, MAX_STRING_LENGTH: kStringMaxLength }

// @ts-ignore
export default <typeof buffer> {
Buffer,
Buffer: Buffer as any as typeof buffer.Buffer,
SlowBuffer: SlowBuffer as any as typeof buffer.SlowBuffer,
kMaxLength,
INSPECT_MAX_BYTES,
Blob,
resolveObjectURL,
SlowBuffer,
transcode,
btoa,
atob,
Expand Down
16 changes: 16 additions & 0 deletions src/runtime/node/stream/consumers/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { notImplemented } from '../../../_internal/utils'
import type * as stramConsumers from 'stream/consumers'

export const arrayBuffer = notImplemented('stream.consumers.arrayBuffer')
export const blob = notImplemented('stream.consumers.blob')
export const buffer = notImplemented('stream.consumers.buffer')
export const text = notImplemented('stream.consumers.text')
export const json = notImplemented('stream.consumers.json')

export default <typeof stramConsumers> {
arrayBuffer,
blob,
buffer,
text,
json
}
66 changes: 48 additions & 18 deletions src/runtime/node/stream/index.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,52 @@
// https://nodejs.org/api/stream.html
import type stream from 'node:stream'
import * as readable from './readable'
import * as writable from './writable'
import * as duplex from './duplex'
import * as transform from './transform'
import * as mock from './mock'
import stream from 'node:stream'
import { Readable } from './readable'
import { Writable } from './writable'
import { Duplex } from './duplex'
import { Transform } from './transform'
import mock from '../../mock/proxy'
import { notImplemented } from '../../_internal/utils'

export * from './readable'
export * from './writable'
export * from './duplex'
export * from './transform'
export * from './mock'
import promises from './promises/index'

// @ts-ignore
export default <typeof stream> {
...readable,
...writable,
...duplex,
...transform,
...mock
export { Readable } from './readable'
export { Writable } from './writable'
export { Duplex } from './duplex'
export { Transform } from './transform'

export const Stream: stream.Stream = mock.__createMock__('Stream')
export const PassThrough: stream.PassThrough = mock.__createMock__('PassThrough')

export const pipeline: typeof stream.pipeline = notImplemented('stream.pipeline') as any
export const finished: typeof stream.finished = notImplemented('stream.finished') as any
export const addAbortSignal: typeof stream.addAbortSignal = notImplemented('stream.addAbortSignal')

// Internal
interface StreamInternal { isDisturbed: any, isReadable: any, compose: any, isErrored: any, destroy: any, _isUint8Array: any, _uint8ArrayToBuffer: any }
export const isDisturbed = notImplemented('stream.isDisturbed')
export const isReadable = notImplemented('stream.isReadable')
export const compose = notImplemented('stream.compose')
export const isErrored = notImplemented('stream.isErrored')
export const destroy = notImplemented('stream.destroy')
export const _isUint8Array = notImplemented('stream._isUint8Array')
export const _uint8ArrayToBuffer = notImplemented('stream._uint8ArrayToBuffer')

export default <typeof stream & StreamInternal> {
Readable: Readable as unknown as typeof stream.Readable,
Writable: Writable as unknown as typeof stream.Writable,
Duplex: Duplex as unknown as typeof stream.Duplex,
Transform: Transform as unknown as typeof stream.Transform,
Stream: Stream as unknown as typeof stream.Stream,
PassThrough: PassThrough as unknown as typeof stream.PassThrough,
pipeline,
finished,
addAbortSignal,
promises,
isDisturbed,
isReadable,
compose,
_uint8ArrayToBuffer,
isErrored,
destroy,
_isUint8Array
}
15 changes: 0 additions & 15 deletions src/runtime/node/stream/mock.ts

This file was deleted.

10 changes: 10 additions & 0 deletions src/runtime/node/stream/promises/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { notImplemented } from '../../../_internal/utils'
import type * as stramPromises from 'stream/promises'

export const finished = notImplemented('stream.promises.finished')
export const pipeline = notImplemented('stream.promises.pipeline')

export default <typeof stramPromises> {
finished,
pipeline
}
40 changes: 40 additions & 0 deletions src/runtime/node/stream/web/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { notImplemented } from '../../../_internal/utils'
import type * as stramWeb from 'stream/web'

export const ReadableStream = globalThis.ReadableStream || notImplemented('stream.web.ReadableStream')
export const ReadableStreamDefaultReader = globalThis.ReadableStreamDefaultReader || notImplemented('stream.web.ReadableStreamDefaultReader')
// @ts-ignore
export const ReadableStreamBYOBReader = globalThis.ReadableStreamBYOBReader || notImplemented('stream.web.ReadableStreamBYOBReader')
// @ts-ignore
export const ReadableStreamBYOBRequest = globalThis.ReadableStreamBYOBRequest || notImplemented('stream.web.ReadableStreamBYOBRequest')
// @ts-ignore
export const ReadableByteStreamController = globalThis.ReadableByteStreamController || notImplemented('stream.web.ReadableByteStreamController')
export const ReadableStreamDefaultController = globalThis.ReadableStreamDefaultController || notImplemented('stream.web.ReadableStreamDefaultController')
export const TransformStream = globalThis.TransformStream || notImplemented('stream.web.TransformStream')
export const TransformStreamDefaultController = globalThis.TransformStreamDefaultController || notImplemented('stream.web.TransformStreamDefaultController')
export const WritableStream = globalThis.WritableStream || notImplemented('stream.web.WritableStream')
export const WritableStreamDefaultWriter = globalThis.WritableStreamDefaultWriter || notImplemented('stream.web.WritableStreamDefaultWriter')
export const WritableStreamDefaultController = globalThis.WritableStreamDefaultController || notImplemented('stream.web.WritableStreamDefaultController')
export const ByteLengthQueuingStrategy = globalThis.ByteLengthQueuingStrategy || notImplemented('stream.web.ByteLengthQueuingStrategy')
export const CountQueuingStrategy = globalThis.CountQueuingStrategy || notImplemented('stream.web.CountQueuingStrategy')
export const TextEncoderStream = globalThis.TextEncoderStream || notImplemented('stream.web.TextEncoderStream')
export const TextDecoderStream = globalThis.TextDecoderStream || notImplemented('stream.web.TextDecoderStream')

// @ts-ignore
export default <typeof stramWeb> {
ReadableStream,
ReadableStreamDefaultReader,
ReadableStreamBYOBReader,
ReadableStreamBYOBRequest,
ReadableByteStreamController,
ReadableStreamDefaultController,
TransformStream,
TransformStreamDefaultController,
WritableStream,
WritableStreamDefaultWriter,
WritableStreamDefaultController,
ByteLengthQueuingStrategy,
CountQueuingStrategy,
TextEncoderStream,
TextDecoderStream
}

0 comments on commit 4206317

Please sign in to comment.