-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(sdk): add support for proxy_http_fetch
- Loading branch information
1 parent
1a01479
commit 1129897
Showing
20 changed files
with
380 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { Process, proxyHttpFetch } from "../../as-sdk/assembly"; | ||
|
||
|
||
export function testProxyHttpFetch(): void { | ||
const response = proxyHttpFetch('http://localhost:5384/proxy/planets/1'); | ||
const fulfilledResponse = response.fulfilled; | ||
const rejectedResponse = response.rejected; | ||
|
||
if (fulfilledResponse) { | ||
const result = String.UTF8.decode(fulfilledResponse.bytes.value.buffer); | ||
|
||
Process.exit_with_message(0, result); | ||
} | ||
|
||
if (rejectedResponse) { | ||
const result = String.UTF8.decode(rejectedResponse.bytes.value.buffer); | ||
|
||
Process.exit_with_message(1, result); | ||
} | ||
|
||
Process.exit_with_message(20, "Something went wrong.."); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { expect, describe, it, mock, beforeEach } from 'bun:test'; | ||
import { executeDrWasm } from '@seda/dev-tools'; | ||
import { readFile } from 'node:fs/promises'; | ||
import { Response } from 'node-fetch'; | ||
|
||
const mockHttpFetch = mock(); | ||
|
||
describe('ProxyHttp', () => { | ||
beforeEach(() => { | ||
mockHttpFetch.mockReset(); | ||
}); | ||
|
||
it.skip('should allow proxy_http_fetch which have a valid signature', async () => { | ||
const wasmBinary = await readFile( | ||
'dist/libs/as-sdk-integration-tests/debug.wasm' | ||
); | ||
|
||
const mockResponse = new Response('"Tatooine"', { | ||
headers: { | ||
'x-seda-signature': '93c67407c95f7d8252d8a28f5a637d57f2088376fcf34751d3ca04324e74d8185d11fe3fb23532f610158393b5678aeda82a56898fa95e0ca4d483e7aa472715', | ||
'x-seda-publickey': '02100efce2a783cc7a3fbf9c5d15d4cc6e263337651312f21a35d30c16cb38f4c3' | ||
}, | ||
}); | ||
|
||
mockHttpFetch.mockResolvedValue(mockResponse); | ||
|
||
const result = await executeDrWasm( | ||
wasmBinary, | ||
Buffer.from('testProxyHttpFetch'), | ||
mockHttpFetch | ||
); | ||
|
||
expect(result.exitCode).toBe(0); | ||
expect(result.result).toEqual(new TextEncoder().encode('"Tatooine"')); | ||
}); | ||
|
||
it.skip('should reject if the proxy_http_fetch has an invalid signature', async () => { | ||
const wasmBinary = await readFile( | ||
'dist/libs/as-sdk-integration-tests/debug.wasm' | ||
); | ||
|
||
const mockResponse = new Response('"Tatooine"', { | ||
statusText: 'mock_ok', | ||
headers: { | ||
'x-seda-signature': '83c67407c95f7d8252d8a28f5a637d57f2088376fcf34751d3ca04324e74d8185d11fe3fb23532f610158393b5678aeda82a56898fa95e0ca4d483e7aa472715', | ||
'x-seda-publickey': '02100efce2a783cc7a3fbf9c5d15d4cc6e263337651312f21a35d30c16cb38f4c3' | ||
}, | ||
}); | ||
|
||
mockHttpFetch.mockResolvedValue(mockResponse); | ||
|
||
const result = await executeDrWasm( | ||
wasmBinary, | ||
Buffer.from('testProxyHttpFetch'), | ||
mockHttpFetch | ||
); | ||
|
||
expect(result.exitCode).toBe(1); | ||
expect(result.result).toEqual(new TextEncoder().encode('Invalid signature')); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { JSON } from "json-as"; | ||
import { decodeHex, encodeHex } from "./hex"; | ||
|
||
@json | ||
class InnerBytes { | ||
type: string = "hex"; | ||
value!: string; | ||
} | ||
|
||
|
||
export class Bytes { | ||
type: string = "hex"; | ||
value: Uint8Array; | ||
|
||
constructor(value: Uint8Array) { | ||
this.value = value; | ||
} | ||
|
||
__SERIALIZE(): string { | ||
const inner = new InnerBytes(); | ||
inner.value = encodeHex(this.value); | ||
|
||
return JSON.stringify(inner); | ||
} | ||
|
||
__INITIALIZE(): void {} | ||
|
||
__DESERIALIZE(data: string, _key_start: i32, _key_end: i32, _outerLoopIndex: i32, _numberValueIndex: i32): bool { | ||
const innerBytes = JSON.parse<InnerBytes>(data); | ||
const buffer = decodeHex(innerBytes.value); | ||
this.value = buffer; | ||
|
||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
|
||
export function encodeHex(array: Uint8Array): string { | ||
let hex = '' | ||
|
||
for (let i = 0; i < array.length; i++) { | ||
hex += array[i].toString(16) | ||
} | ||
|
||
return hex | ||
} | ||
|
||
export function decodeHex(data: string): Uint8Array { | ||
let array = new Uint8Array(data.length >>> 1) | ||
|
||
for (let i = 0; i < data.length >>> 1; ++i) { | ||
array.fill(i32(parseInt('0x' + data.substr(i * 2, 2), 16)), i, i + 1) | ||
} | ||
|
||
return array; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { JSON } from 'json-as/assembly'; | ||
import { HttpFetchOptions, HttpFetch, HttpResponse } from "./http"; | ||
import { call_result_write, proxy_http_fetch } from './bindings/seda_v1'; | ||
import { PromiseStatus } from './promise'; | ||
import { Console } from './console'; | ||
|
||
export function proxyHttpFetch(url: string, options: HttpFetchOptions = new HttpFetchOptions()): PromiseStatus<HttpResponse, HttpResponse> { | ||
const action = new HttpFetch(url, options); | ||
const actionStr = JSON.stringify(action); | ||
|
||
const buffer = String.UTF8.encode(actionStr); | ||
const utf8ptr = changetype<usize>(buffer); | ||
|
||
const responseLength = proxy_http_fetch(utf8ptr, buffer.byteLength); | ||
const responseBuffer = new ArrayBuffer(responseLength); | ||
const responseBufferPtr = changetype<usize>(responseBuffer); | ||
|
||
call_result_write(responseBufferPtr, responseLength); | ||
|
||
const response = String.UTF8.decode(responseBuffer); | ||
|
||
return PromiseStatus.fromStr( | ||
response, | ||
new HttpResponse(), | ||
new HttpResponse(), | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.