Skip to content

Commit

Permalink
segmented-object: deprecate makeChunkSource
Browse files Browse the repository at this point in the history
  • Loading branch information
yoursunny committed Feb 4, 2024
1 parent 60171d5 commit d339fb7
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 13 deletions.
22 changes: 15 additions & 7 deletions pkg/segmented-object/src/serve/chunk-source/make.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,27 @@ import { BufferChunkSource } from "./buffer";
import type { ChunkOptions } from "./common";
import { IterableChunkSource } from "./iterable";

/**
* Create a {@link BufferChunkSource}.
* @deprecated Use of this function is discouraged because it pulls in `ChunkSource` subclasses
* not needed by your application. Instead, construct {@link BufferChunkSource} directly.
*/
export function makeChunkSource(input: Uint8Array, opts?: ChunkOptions): BufferChunkSource;

/**
* Create a {@link BlobChunkSource}.
* @deprecated Use of this function is discouraged because it pulls in `ChunkSource` subclasses
* not needed by your application. Instead, construct {@link BlobChunkSource} directly.
*/
export function makeChunkSource(input: Blob | NodeBlob, opts?: ChunkOptions): BlobChunkSource;

export function makeChunkSource(input: AnyIterable<Uint8Array> | NodeJS.ReadableStream, opts?: ChunkOptions): IterableChunkSource;

/**
* Create a chunk source, auto detecting input type.
*
* @remarks
* Use of this function is discouraged as it pulls in `ChunkSource` subclasses not needed by
* your application. It's recommended to construct a `ChunkSource` subclass directly.
* Create a {@link IterableChunkSource}.
* @deprecated Use of this function is discouraged because it pulls in `ChunkSource` subclasses
* not needed by your application. Instead, construct {@link IterableChunkSource} directly.
*/
export function makeChunkSource(input: AnyIterable<Uint8Array> | NodeJS.ReadableStream, opts?: ChunkOptions): IterableChunkSource;

export function makeChunkSource(input: Uint8Array | Blob | NodeBlob | AnyIterable<Uint8Array> | NodeJS.ReadableStream, opts?: ChunkOptions) {
if (input instanceof Uint8Array) {
return new BufferChunkSource(input, opts);
Expand Down
12 changes: 6 additions & 6 deletions pkg/segmented-object/tests/serve-fetch.t.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { BufferReadableMock, BufferWritableMock } from "stream-mock";
import { collect, consume } from "streaming-iterables";
import { afterEach, beforeAll, beforeEach, describe, expect, test, vi } from "vitest";

import { BlobChunkSource, BufferChunkSource, fetch, FileChunkSource, IterableChunkSource, makeChunkSource, serve, StreamChunkSource } from "..";
import { BlobChunkSource, BufferChunkSource, fetch, FileChunkSource, IterableChunkSource, serve, StreamChunkSource } from "..";
import { makeObjectBody } from "../test-fixture/object-body";

const fwOpts: Forwarder.Options = { dataNoTokenMatch: false };
Expand All @@ -35,7 +35,7 @@ async function* generateChunksSlowly() {
}

test("buffer to buffer", async () => {
const chunkSource = makeChunkSource(objectBody);
const chunkSource = new BufferChunkSource(objectBody);
expect(chunkSource).toBeInstanceOf(BufferChunkSource);
const server = serve("/R", chunkSource);
closers.push(server);
Expand All @@ -47,7 +47,7 @@ test("buffer to buffer", async () => {
});

test("blob to chunks", async () => {
const chunkSource = makeChunkSource(new Blob([objectBody]));
const chunkSource = new BlobChunkSource(new Blob([objectBody]));
expect(chunkSource).toBeInstanceOf(BlobChunkSource);
const server = serve("/R", chunkSource);
closers.push(server);
Expand All @@ -60,7 +60,7 @@ test("blob to chunks", async () => {

test("stream to stream", async () => {
const src = new BufferReadableMock([objectBody]);
const chunkSource = makeChunkSource(src);
const chunkSource = new StreamChunkSource(src);
expect(chunkSource).toBeInstanceOf(StreamChunkSource);
const server = serve("/R", chunkSource);
closers.push(server);
Expand Down Expand Up @@ -89,7 +89,7 @@ describe("file source", () => {
});

test("iterable to unordered", async () => {
const chunkSource = makeChunkSource((async function*() {
const chunkSource = new IterableChunkSource((async function*() {
const yieldSizes = [5000, 7000, 20000];
let i = -1;
for (let offset = 0; offset < objectBody.length;) {
Expand Down Expand Up @@ -119,7 +119,7 @@ test("iterable to unordered", async () => {
});

test("ranged", async () => {
const chunkSource = makeChunkSource(objectBody, { chunkSize: 1024 }); // 1024 segments
const chunkSource = new BufferChunkSource(objectBody, { chunkSize: 1024 }); // 1024 segments
expect(chunkSource).toBeInstanceOf(BufferChunkSource);
const server = serve(new Name("/R"), chunkSource);
closers.push(server);
Expand Down

0 comments on commit d339fb7

Please sign in to comment.