Skip to content

Commit 07a9916

Browse files
committed
feat: Adds new chunk() method as part of example
1 parent 514f754 commit 07a9916

5 files changed

Lines changed: 83 additions & 24 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
"@commitlint/cli": "^19.8.1",
2929
"@commitlint/config-conventional": "^19.8.1",
3030
"@extism/js-pdk": "^1.1.1",
31-
"@taskless/loader": "0.0.30",
31+
"@taskless/loader": "0.0.35",
3232
"@types/node": "^22.16.0",
3333
"esbuild": "^0.19.12",
3434
"husky": "^9.1.7",

pnpm-lock.yaml

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

src/helpers.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
import { type PluginOutput, type PluginInput } from "@taskless/loader";
22
import { type PartialDeep } from "type-fest";
3-
import { type Pack } from "./__generated__/pack.js";
4-
5-
type PackConfiguration = Pick<Pack, "configuration">;
63

74
export const readInput = <
85
TContext extends Record<string, unknown> = Record<string, unknown>,
@@ -13,8 +10,7 @@ export const readInput = <
1310
TContext,
1411
TRequestBody,
1512
TResponseBody
16-
> &
17-
PackConfiguration;
13+
>;
1814
};
1915

2016
export const writeOutput = <

src/index.d.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,17 @@
11
// eslint-disable-next-line @typescript-eslint/triple-slash-reference
22
/// <reference lib="es2021" />
33

4+
/*
5+
Each export here must also be an export inside of your src/index.ts file.
6+
Extism layers this defintion on top opf your exports in order to create
7+
a map of function names to memory addresses.
8+
9+
Failure to keep this in sync results in calling functions that do not
10+
exist, or calling one function and getting the result of another function.
11+
*/
12+
413
declare module "main" {
514
export function pre(): I32;
615
export function post(): I32;
16+
export function chunk(): I32;
717
}

src/index.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import { manifest } from "./manifest.js";
44
/** left as an example of passing context into outputs */
55
type Context = {
66
fromPre: string;
7+
currentChunk?: string;
8+
lines?: string[];
79
};
810

911
/**
@@ -37,6 +39,49 @@ export function pre() {
3739
});
3840
}
3941

42+
/**
43+
* chunk() is called for each chunk of the response body
44+
* it's data is base64 encoded
45+
*/
46+
export function chunk() {
47+
const input = readInput<Context>();
48+
const chunk = input.chunk;
49+
50+
if (!chunk) {
51+
return;
52+
}
53+
54+
// take base64 encoded chunk and decode it
55+
const decoder = new TextDecoder("utf8");
56+
const decodedChunk = decoder.decode(Host.base64ToArrayBuffer(chunk));
57+
const current = (input.context.currentChunk ?? "") + decodedChunk;
58+
59+
// if no newlines, save the current chunk and return
60+
if (!current.includes("\n")) {
61+
writeOutput<Context>({
62+
context: {
63+
currentChunk: current,
64+
},
65+
});
66+
67+
return;
68+
}
69+
70+
// split the current chunk by newlines, add to the context lines
71+
const lines = (input.context.lines ?? [])
72+
.concat(current.split("\n"))
73+
.map((l) => l.trim())
74+
.filter((l) => l.length > 0);
75+
76+
// save the lines and reset the current chunk
77+
writeOutput<Context>({
78+
context: {
79+
currentChunk: "",
80+
lines,
81+
},
82+
});
83+
}
84+
4085
/**
4186
* post() is called after the request is sent to the host
4287
* and contains the response body
@@ -53,6 +98,7 @@ export function post() {
5398
// explicit capture from the prior context
5499
testPostFromPre: input.context.fromPre,
55100
testResponseData: responseDataC,
101+
testChunk: input.context.lines?.join("~~~"),
56102
},
57103
});
58104
}

0 commit comments

Comments
 (0)