mirror of
https://github.com/webstudio-is/webstudio.git
synced 2026-11-09 05:26:24 +09:00
Use stream consumers while uploading (#2064)
Looks like node now provides nice utilities to convert stream into json, buffer, text etc. Super handy https://nodejs.org/docs/latest-v18.x/api/webstreams.html#utility-consumers Cloudflare node compat supports this as well https://developers.cloudflare.com/workers/runtime-apis/nodejs/streams/
This commit is contained in:
1 parent
7d635c4940
commit
fe50a537fe
3 files changed
+5
-13
No files matched your search
@@ -1,7 +1,7 @@
|
||||
import { mkdir, writeFile } from "node:fs/promises";
|
||||
import { dirname, resolve } from "node:path";
|
||||
import { buffer } from "node:stream/consumers";
|
||||
import { getAssetData } from "../../utils/get-asset-data";
|
||||
import { toUint8Array } from "../../utils/to-uint8-array";
|
||||
import { createSizeLimiter } from "../../utils/size-limiter";
|
||||
|
||||
export const uploadToFs = async ({
|
||||
@@ -23,7 +23,7 @@ export const uploadToFs = async ({
|
||||
await mkdir(dirname(filepath), { recursive: true }).catch(() => {});
|
||||
const limitSize = createSizeLimiter(maxSize, name);
|
||||
|
||||
const data = await toUint8Array(limitSize(dataStream));
|
||||
const data = await buffer(limitSize(dataStream));
|
||||
await writeFile(filepath, data);
|
||||
|
||||
const assetData = await getAssetData({
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { arrayBuffer } from "node:stream/consumers";
|
||||
import type { SignatureV4 } from "@smithy/signature-v4";
|
||||
import { toUint8Array } from "../../utils/to-uint8-array";
|
||||
import { getAssetData } from "../../utils/get-asset-data";
|
||||
import { createSizeLimiter } from "../../utils/size-limiter";
|
||||
|
||||
@@ -28,7 +28,7 @@ export const uploadToS3 = async ({
|
||||
// this has to be a stream that goes directly to s3
|
||||
// Size check has to happen as you stream and interrupted when size is too big
|
||||
// Also check if S3 client has an option to check the size limit
|
||||
const data = await toUint8Array(limitSize(dataStream));
|
||||
const data = await arrayBuffer(limitSize(dataStream));
|
||||
|
||||
const url = new URL(`/${bucket}/${name}`, endpoint);
|
||||
|
||||
@@ -64,7 +64,7 @@ export const uploadToS3 = async ({
|
||||
const assetData = await getAssetData({
|
||||
type: type.startsWith("image") ? "image" : "font",
|
||||
size: data.byteLength,
|
||||
data,
|
||||
data: new Uint8Array(data),
|
||||
});
|
||||
|
||||
return assetData;
|
||||
|
||||
@@ -1,8 +0,0 @@
|
||||
export const toUint8Array = async (data: AsyncIterable<ArrayBuffer>) => {
|
||||
const result = [];
|
||||
for await (const chunk of data) {
|
||||
result.push(Buffer.from(chunk));
|
||||
}
|
||||
|
||||
return new Uint8Array(Buffer.concat(result));
|
||||
};
|
||||
Reference in new issue
Block a user