Skip to content

Commit 3f0aafd

Browse files
committed
chore: tweaks
1 parent 86391de commit 3f0aafd

File tree

8 files changed

+54
-39
lines changed

8 files changed

+54
-39
lines changed

example/server/createServer.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ export function createServer(payload: string | Bun.BufferSource, port: number) {
1414
// Handle POST /upload
1515
if (req.method === 'POST' && req.url.endsWith('/upload')) {
1616
try {
17-
console.log('req.body', req.body)
17+
console.log('Waiting for body...')
18+
1819
if (!req.body) {
1920
return new Response('No body provided', { status: 400 })
2021
}

package-lock.json

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

packages/react-native-fast-io/package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,15 @@
1919
],
2020
"author": "Mike Grabowski <grabbou@gmail.com>",
2121
"peerDependencies": {
22-
"react-native-nitro-modules": "^0.15.0",
23-
"react-native": "^0.76.0"
22+
"react-native": "^0.76.0",
23+
"react-native-nitro-modules": "^0.15.0"
2424
},
2525
"dependencies": {
2626
"event-target-shim": "^6.0.2",
2727
"web-streams-polyfill": "^4.0.0"
2828
},
2929
"devDependencies": {
30+
"@types/text-encoding": "^0.0.39",
3031
"nitro-codegen": "^0.15.0"
3132
},
3233
"keywords": [

packages/react-native-fast-io/src/w3c/blob.ts

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,16 @@ export class Blob {
2323
* https://w3c.github.io/FileAPI/#attributes-blob
2424
*/
2525
readonly type: string
26+
27+
protected _size: number
2628
get size(): number {
27-
return calculateSize(this.parts)
29+
return this._size
2830
}
2931

3032
constructor(parts: Array<BlobPart> = [], options: BlobPropertyBag = {}) {
3133
this.parts = parts
3234
this.type = options?.type?.toLowerCase() || ''
35+
this._size = calculateSize(parts)
3336
}
3437

3538
/**
@@ -61,12 +64,8 @@ export class Blob {
6164
async start(controller) {
6265
try {
6366
for (const stream of streams) {
64-
const reader = stream.getReader()
65-
// eslint-disable-next-line no-constant-condition
66-
while (true) {
67-
const { done, value } = await reader.read()
68-
if (done) break
69-
controller.enqueue(value)
67+
for await (const chunk of stream) {
68+
controller.enqueue(chunk)
7069
}
7170
}
7271
controller.close()
@@ -81,6 +80,14 @@ export class Blob {
8180
* https://w3c.github.io/FileAPI/#arraybuffer-method-algo
8281
*/
8382
async arrayBuffer(): Promise<ArrayBuffer> {
83+
const view = await this.bytes()
84+
return view.buffer
85+
}
86+
87+
/**
88+
* https://w3c.github.io/FileAPI/#bytes-method-algo
89+
*/
90+
async bytes(): Promise<Uint8Array> {
8491
const result = new ArrayBuffer(this.size)
8592
const view = new Uint8Array(result)
8693

@@ -90,22 +97,14 @@ export class Blob {
9097
offset += chunk.length
9198
}
9299

93-
return result
94-
}
95-
96-
/**
97-
* https://w3c.github.io/FileAPI/#bytes-method-algo
98-
*/
99-
async bytes(): Promise<Uint8Array> {
100-
const buffer = await this.arrayBuffer()
101-
return new Uint8Array(buffer)
100+
return view
102101
}
103102

104103
/**
105104
* https://w3c.github.io/FileAPI/#text-method-algo
106105
*/
107106
async text(): Promise<string> {
108-
const buffer = await this.arrayBuffer()
107+
const buffer = await this.bytes()
109108
return new TextDecoder().decode(buffer)
110109
}
111110

packages/react-native-fast-io/src/w3c/fs.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { ReadableStream } from 'web-streams-polyfill'
22

3-
import { FileSystem, Metadata } from '../native/fs.nitro'
3+
import { FileSystem } from '../native/fs.nitro'
44
import { Blob, BlobPart, BlobPropertyBag } from './blob'
55
import { toReadableStream } from './streams'
66

@@ -35,11 +35,6 @@ class File extends Blob {
3535
class NativeFile extends File {
3636
nativeStream: ReadableStream<Uint8Array>
3737

38-
_size: number
39-
override get size(): number {
40-
return this._size
41-
}
42-
4338
constructor(path: string) {
4439
const inputStream = FileSystem.createInputStream(path)
4540
const metadata = FileSystem.getFileMetadata(path)
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* TextEncoder is available in Hermes
3+
*/
4+
declare class TextEncoder {
5+
constructor()
6+
encode(input?: string): Uint8Array
7+
}
8+
9+
/**
10+
* TextDecoder is not available in Hermes yet. This must be polyfilled.
11+
* https://github.com/facebook/hermes/issues/1403
12+
*/
13+
declare class TextDecoder {
14+
constructor()
15+
decode(input?: Uint8Array): string
16+
}

packages/react-native-fast-io/src/w3c/streams.ts

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,23 @@ import { ReadableStream, WritableStream } from 'web-streams-polyfill'
22

33
import { InputStream, OutputStream, PassThroughStream } from '../native/streams.nitro'
44

5+
const CHUNK_SIZE = 1024 * 64
6+
57
export const toReadableStream = (inputStream: InputStream) => {
68
const stream = new ReadableStream<Uint8Array>({
7-
async start() {
9+
start() {
810
inputStream.open()
911
},
10-
1112
pull(controller) {
12-
const chunkSize = 1024 * 64
13-
const buffer = new ArrayBuffer(chunkSize)
13+
const buffer = new ArrayBuffer(CHUNK_SIZE)
1414

1515
if (!inputStream.hasBytesAvailable()) {
1616
inputStream.close()
1717
controller.close()
1818
return
1919
}
2020

21-
const bytesRead = inputStream.read(buffer, chunkSize)
21+
const bytesRead = inputStream.read(buffer, CHUNK_SIZE)
2222
if (bytesRead < 0) {
2323
inputStream.close()
2424
controller.error('Error reading from stream.')
@@ -29,7 +29,6 @@ export const toReadableStream = (inputStream: InputStream) => {
2929
controller.enqueue(new Uint8Array(buffer.slice(0, bytesRead)))
3030
}
3131
},
32-
3332
cancel() {
3433
inputStream.close()
3534
},
@@ -43,22 +42,18 @@ export const toWritableStream = (outputStream: OutputStream) => {
4342
start() {
4443
outputStream.open()
4544
},
46-
47-
async write(chunk: ArrayBuffer) {
45+
write(chunk: Uint8Array) {
4846
if (!outputStream.hasSpaceAvailable()) {
4947
throw new Error('No space available in output stream')
5048
}
51-
52-
const bytesWritten = outputStream.write(chunk, chunk.byteLength)
49+
const bytesWritten = outputStream.write(chunk.buffer, chunk.byteLength)
5350
if (bytesWritten < 0) {
5451
throw new Error('Failed to write to output stream')
5552
}
5653
},
57-
5854
close() {
5955
outputStream.close()
6056
},
61-
6257
abort() {
6358
outputStream.close()
6459
},

packages/react-native-fast-io/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"extends": "../../tsconfig.json",
33
"compilerOptions": {
44
"paths": {
5-
"event-target-shim": ["./node_modules/event-target-shim"]
5+
"event-target-shim": ["./node_modules/event-target-shim"],
66
}
77
}
88
}

0 commit comments

Comments
 (0)