redstuff
redstuff
DDeno
Created by function on 10/11/2024 in #help
Deno.serve() + sharp + streams
I will update you later - if I get time to look into this further.
34 replies
DDeno
Created by function on 10/11/2024 in #help
Deno.serve() + sharp + streams
if I had time.
34 replies
DDeno
Created by function on 10/11/2024 in #help
Deno.serve() + sharp + streams
I would experment with that...
34 replies
DDeno
Created by function on 10/11/2024 in #help
Deno.serve() + sharp + streams
sorry, its there 🙂
34 replies
DDeno
Created by function on 10/11/2024 in #help
Deno.serve() + sharp + streams
...and slower
34 replies
DDeno
Created by function on 10/11/2024 in #help
Deno.serve() + sharp + streams
You should probably shime in here about it not working.~
34 replies
DDeno
Created by function on 10/11/2024 in #help
Deno.serve() + sharp + streams
Ouch - that did not work for us either.
34 replies
DDeno
Created by function on 10/11/2024 in #help
Deno.serve() + sharp + streams
I think best attack vector here - is to make sharp deno streams compatible
34 replies
DDeno
Created by function on 10/11/2024 in #help
Deno.serve() + sharp + streams
Mhm, probably need some smartness here for it to be faster - above my paygrade. Define slower though?
34 replies
DDeno
Created by function on 10/11/2024 in #help
Deno.serve() + sharp + streams
aha, sure - this example was when you have a deno stream you want to stream to a node stream. The result indeed has to be converted back. I can share our attempt, its indeed not perfect:
export function toDenoReadableStream(stream: Readable): ReadableStream<Uint8Array> {
return new ReadableStream({
start(controller) {
stream.on("data", (chunk) => {
controller.enqueue(chunk);
});

stream.on("error", (err) => {
// logger.error(err);
controller.error(err);
});

stream.on("end", () => {
try {
// Interacting with the controller if already "closed" will throw an error
// https://developer.mozilla.org/en-US/docs/Web/API/ReadableStreamDefaultController/error
controller.close();
} catch {
// Ignore error when closing - if already closed?
}
});
},
cancel(reason) {
stream.destroy(reason);
},
});
}
export function toDenoReadableStream(stream: Readable): ReadableStream<Uint8Array> {
return new ReadableStream({
start(controller) {
stream.on("data", (chunk) => {
controller.enqueue(chunk);
});

stream.on("error", (err) => {
// logger.error(err);
controller.error(err);
});

stream.on("end", () => {
try {
// Interacting with the controller if already "closed" will throw an error
// https://developer.mozilla.org/en-US/docs/Web/API/ReadableStreamDefaultController/error
controller.close();
} catch {
// Ignore error when closing - if already closed?
}
});
},
cancel(reason) {
stream.destroy(reason);
},
});
}
34 replies
DDeno
Created by function on 10/11/2024 in #help
Deno.serve() + sharp + streams
You have to convert them to the expected stream of the library, e.g. deno to node streams expected in sharp:
function toNodeReadable(stream: ReadableStream<Uint8Array>): Readable {
const reader = stream.getReader();
return new Readable({
async read() {
try {
const { done, value } = await reader.read();
if (done) {
this.push(null);
} else {
this.push(value);
}
} catch (error) {
this.destroy(error instanceof Error ? error : undefined);
}
},
});
}

toNodeReadable(file.stream()).pipe(sharp().jpeg({ quality: 100 })),
function toNodeReadable(stream: ReadableStream<Uint8Array>): Readable {
const reader = stream.getReader();
return new Readable({
async read() {
try {
const { done, value } = await reader.read();
if (done) {
this.push(null);
} else {
this.push(value);
}
} catch (error) {
this.destroy(error instanceof Error ? error : undefined);
}
},
});
}

toNodeReadable(file.stream()).pipe(sharp().jpeg({ quality: 100 })),
34 replies