Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add JS resp-set-header and request-set-field bindings #272

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 57 additions & 47 deletions api/typescript/src/bindings/env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,59 +30,67 @@ export enum QueryType {
}
export class Env {

// The WebAssembly instance that this class is operating with.
// This is only available after the `instantiate` method has
// been called.
/**
* The WebAssembly instance that this class is operating with.
* This is only available after the `instantiate` method has
* been called.
*/
instance: WebAssembly.Instance;

// Constructs a new instance with internal state necessary to
// manage a wasm instance.
//
// Note that this does not actually instantiate the WebAssembly
// instance or module, you'll need to call the `instantiate`
// method below to "activate" this class.
/**
* Constructs a new instance with internal state necessary to
* manage a wasm instance.
*
* Note that this does not actually instantiate the WebAssembly
* instance or module, you'll need to call the `instantiate`
* method below to "activate" this class.
*/
constructor();

// This is a low-level method which can be used to add any
// intrinsics necessary for this instance to operate to an
// import object.
//
// The `import` object given here is expected to be used later
// to actually instantiate the module this class corresponds to.
// If the `instantiate` method below actually does the
// instantiation then there's no need to call this method, but
// if you're instantiating manually elsewhere then this can be
// used to prepare the import object for external instantiation.
/**
* This is a low-level method which can be used to add any
* intrinsics necessary for this instance to operate to an
* import object.
*
* The `import` object given here is expected to be used later
* to actually instantiate the module this class corresponds to.
* If the `instantiate` method below actually does the
* instantiation then there's no need to call this method, but
* if you're instantiating manually elsewhere then this can be
* used to prepare the import object for external instantiation.
*/
addToImports(imports: any): void;

// Initializes this object with the provided WebAssembly
// module/instance.
//
// This is intended to be a flexible method of instantiating
// and completion of the initialization of this class. This
// method must be called before interacting with the
// WebAssembly object.
//
// The first argument to this method is where to get the
// wasm from. This can be a whole bunch of different types,
// for example:
//
// * A precompiled `WebAssembly.Module`
// * A typed array buffer containing the wasm bytecode.
// * A `Promise` of a `Response` which is used with
// `instantiateStreaming`
// * A `Response` itself used with `instantiateStreaming`.
// * An already instantiated `WebAssembly.Instance`
//
// If necessary the module is compiled, and if necessary the
// module is instantiated. Whether or not it's necessary
// depends on the type of argument provided to
// instantiation.
//
// If instantiation is performed then the `imports` object
// passed here is the list of imports used to instantiate
// the instance. This method may add its own intrinsics to
// this `imports` object too.
/**
* Initializes this object with the provided WebAssembly
* module/instance.
*
* This is intended to be a flexible method of instantiating
* and completion of the initialization of this class. This
* method must be called before interacting with the
* WebAssembly object.
*
* The first argument to this method is where to get the
* wasm from. This can be a whole bunch of different types,
* for example:
*
* * A precompiled `WebAssembly.Module`
* * A typed array buffer containing the wasm bytecode.
* * A `Promise` of a `Response` which is used with
* `instantiateStreaming`
* * A `Response` itself used with `instantiateStreaming`.
* * An already instantiated `WebAssembly.Instance`
*
* If necessary the module is compiled, and if necessary the
* module is instantiated. Whether or not it's necessary
* depends on the type of argument provided to
* instantiation.
*
* If instantiation is performed then the `imports` object
* passed here is the list of imports used to instantiate
* the instance. This method may add its own intrinsics to
* this `imports` object too.
*/
instantiate(
module: WebAssembly.Module | BufferSource | Promise<Response> | Response | WebAssembly.Instance,
imports?: any,
Expand All @@ -95,6 +103,8 @@ export class Env {
cacheSet(key: string, value: Uint8Array, ttl: number, ident: number): number;
cacheGet(key: string, ident: number): number;
requestGetField(fieldType: FieldType, key: string, ident: number): number;
requestSetField(fieldType: FieldType, key: string, value: Uint8Array, ident: number): number;
respSetHeader(key: string, value: Uint8Array, ident: number): void;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm any reason value does not simply take a string here? I would think that's more appropriate for header values 🤔

Rust seems to take also set_header(key: &str, val: &str).

getStaticFile(name: string, ident: number): number;
dbExec(queryType: QueryType, name: string, ident: number): number;
getFfiResult(ptr: number, ident: number): number;
Expand Down
31 changes: 31 additions & 0 deletions api/typescript/src/bindings/env.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,15 @@ export class Env {
const ptr0 = realloc(0, 0, 1, len0 * 1);
(new Uint8Array(memory.buffer, ptr0, len0 * 1)).set(new Uint8Array(val0.buffer, val0.byteOffset, len0 * 1));
this._exports['return-result'](ptr0, len0, clamp_host(arg1, 0, 4294967295));
return undefined;
}
returnError(arg0, arg1, arg2) {
const memory = this._exports.memory;
const realloc = this._exports["canonical_abi_realloc"];
const ptr0 = utf8_encode(arg1, realloc, memory);
const len0 = UTF8_ENCODED_LEN;
this._exports['return-error'](clamp_host(arg0, -2147483648, 2147483647), ptr0, len0, clamp_host(arg2, 0, 4294967295));
return undefined;
}
logMsg(arg0, arg1, arg2) {
const memory = this._exports.memory;
Expand All @@ -97,6 +99,7 @@ export class Env {
if (!(variant1 in LogLevel))
throw new RangeError("invalid variant specified for LogLevel");
this._exports['log-msg'](ptr0, len0, Number.isInteger(variant1) ? variant1 : LogLevel[variant1], clamp_host(arg2, 0, 4294967295));
return undefined;
}
fetchUrl(arg0, arg1, arg2, arg3) {
const memory = this._exports.memory;
Expand Down Expand Up @@ -154,6 +157,33 @@ export class Env {
const ret = this._exports['request-get-field'](Number.isInteger(variant0) ? variant0 : FieldType[variant0], ptr1, len1, clamp_host(arg2, 0, 4294967295));
return ret;
}
requestSetField(arg0, arg1, arg2, arg3) {
const memory = this._exports.memory;
const realloc = this._exports["canonical_abi_realloc"];
const variant0 = arg0;
if (!(variant0 in FieldType))
throw new RangeError("invalid variant specified for FieldType");
const ptr1 = utf8_encode(arg1, realloc, memory);
const len1 = UTF8_ENCODED_LEN;
const val2 = arg2;
const len2 = val2.length;
const ptr2 = realloc(0, 0, 1, len2 * 1);
(new Uint8Array(memory.buffer, ptr2, len2 * 1)).set(new Uint8Array(val2.buffer, val2.byteOffset, len2 * 1));
const ret = this._exports['request-set-field'](Number.isInteger(variant0) ? variant0 : FieldType[variant0], ptr1, len1, ptr2, len2, clamp_host(arg3, 0, 4294967295));
return ret;
}
respSetHeader(arg0, arg1, arg2) {
const memory = this._exports.memory;
const realloc = this._exports["canonical_abi_realloc"];
const ptr0 = utf8_encode(arg0, realloc, memory);
const len0 = UTF8_ENCODED_LEN;
const val1 = arg1;
const len1 = val1.length;
const ptr1 = realloc(0, 0, 1, len1 * 1);
(new Uint8Array(memory.buffer, ptr1, len1 * 1)).set(new Uint8Array(val1.buffer, val1.byteOffset, len1 * 1));
this._exports['resp-set-header'](ptr0, len0, ptr1, len1, clamp_host(arg2, 0, 4294967295));
return undefined;
}
getStaticFile(arg0, arg1) {
const memory = this._exports.memory;
const realloc = this._exports["canonical_abi_realloc"];
Expand Down Expand Up @@ -195,5 +225,6 @@ export class Env {
const ptr1 = utf8_encode(arg1, realloc, memory);
const len1 = UTF8_ENCODED_LEN;
this._exports['return-abort'](ptr0, len0, ptr1, len1, clamp_host(arg2, 0, 4294967295), clamp_host(arg3, 0, 4294967295), clamp_host(arg4, 0, 4294967295));
return undefined;
}
}