-
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(vm): Implemented HTTP fetch + VM Adapters using Atomics and Shar…
…edArrayBuffers
- Loading branch information
1 parent
13162a7
commit c074579
Showing
14 changed files
with
1,512 additions
and
3,819 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,42 @@ | ||
import { HttpFetchAction, VmAdapter } from "./types/vm-adapter"; | ||
import fetch from "node-fetch"; | ||
import type { HttpFetchAction } from './types/vm-actions'; | ||
import { HttpFetchResponse } from './types/vm-actions.js'; | ||
import type { VmAdapter } from './types/vm-adapter'; | ||
import fetch from 'node-fetch'; | ||
import { PromiseStatus } from './types/vm-promise.js'; | ||
|
||
export default class DefaultVmAdapter implements VmAdapter { | ||
async http_fetch(action: HttpFetchAction): Promise<Response> { | ||
console.log(action); | ||
async httpFetch(action: HttpFetchAction): Promise<PromiseStatus<HttpFetchResponse>> { | ||
try { | ||
const response = await fetch(new URL(action.url), { | ||
method: action.options.method.toUpperCase(), | ||
headers: action.options.headers, | ||
body: action.options.body | ||
? Buffer.from(action.options.body) | ||
: undefined, | ||
}); | ||
|
||
return new Response(); | ||
const bufferResponse = await response.arrayBuffer(); | ||
const httpResponse = new HttpFetchResponse({ | ||
bytes: Array.from(new Uint8Array(bufferResponse)), | ||
content_length: response.size, | ||
headers: Object.fromEntries(response.headers.entries()), | ||
status: response.status, | ||
url: response.url, | ||
}); | ||
|
||
return PromiseStatus.fulfilled(httpResponse); | ||
} catch (error) { | ||
const stringifiedError = '' + error; | ||
|
||
console.error('@default-vm-adapter: ', stringifiedError); | ||
|
||
return PromiseStatus.rejected(new HttpFetchResponse({ | ||
bytes: Array.from(new TextEncoder().encode(stringifiedError)), | ||
content_length: stringifiedError.length, | ||
headers: {}, | ||
status: 0, | ||
url: '', | ||
})); | ||
} | ||
} | ||
} |
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,51 @@ | ||
import type { ToBuffer } from "./vm-promise"; | ||
|
||
enum HttpFetchMethod { | ||
Options = 'Options', | ||
Get = 'Get', | ||
Post = 'Post', | ||
Put = 'Put', | ||
Delete = 'Delete', | ||
Head = 'Head', | ||
Trace = 'Trace', | ||
Connect = 'Connect', | ||
Patch = 'Patch', | ||
} | ||
|
||
export interface HttpFetchOptions { | ||
method: HttpFetchMethod; | ||
headers: { [key: string]: string }; | ||
body?: Uint8Array; | ||
} | ||
|
||
export interface HttpFetchAction { | ||
url: string; | ||
options: HttpFetchOptions; | ||
} | ||
|
||
export interface HttpFetchResponseData { | ||
/** HTTP Status code */ | ||
status: number; | ||
|
||
/** Response headers */ | ||
headers: { [key: string]: string }; | ||
|
||
/** Response body in bytes */ | ||
bytes: number[]; | ||
|
||
/** The final URL that was resolved */ | ||
url: string; | ||
|
||
/** The byte length of the response */ | ||
content_length: number; | ||
} | ||
|
||
export class HttpFetchResponse implements ToBuffer { | ||
constructor(public data: HttpFetchResponseData) {} | ||
|
||
toBuffer(): Uint8Array { | ||
return new TextEncoder().encode(JSON.stringify(this.data)); | ||
} | ||
} | ||
|
||
export type VmAction = HttpFetchAction; |
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 |
---|---|---|
@@ -1,26 +1,7 @@ | ||
enum HttpFetchMethod { | ||
Options, | ||
Get, | ||
Post, | ||
Put, | ||
Delete, | ||
Head, | ||
Trace, | ||
Connect, | ||
Patch, | ||
} | ||
|
||
export interface HttpFetchOptions { | ||
method: HttpFetchMethod, | ||
headers: Map<string, string>, | ||
body?: Uint8Array, | ||
} | ||
import type { HttpFetchAction, HttpFetchResponse } from "./vm-actions"; | ||
import { PromiseStatus } from "./vm-promise.js"; | ||
|
||
export interface HttpFetchAction { | ||
url: string, | ||
options: HttpFetchOptions, | ||
} | ||
|
||
export interface VmAdapter { | ||
http_fetch(action: HttpFetchAction): Promise<Response>; | ||
httpFetch(action: HttpFetchAction): Promise<PromiseStatus<HttpFetchResponse>>; | ||
} |
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,38 @@ | ||
interface PromiseStatusResult { | ||
/** Actually a Uint8[] */ | ||
Fulfilled?: number[]; | ||
/** Actually a Uint8[] */ | ||
Rejected?: number[]; | ||
} | ||
|
||
export interface ToBuffer { | ||
toBuffer(): Uint8Array; | ||
} | ||
|
||
export class PromiseStatus<T> { | ||
private constructor(private value: PromiseStatusResult) {} | ||
|
||
static rejected<T extends ToBuffer>(value: T): PromiseStatus<T> { | ||
return new PromiseStatus({ | ||
Rejected: Array.from(value.toBuffer()), | ||
}); | ||
} | ||
|
||
static fulfilled<T extends ToBuffer>(value?: T): PromiseStatus<T> { | ||
return new PromiseStatus({ | ||
Fulfilled: Array.from(value?.toBuffer() ?? []), | ||
}); | ||
} | ||
|
||
get length() { | ||
return this.toJSON().length; | ||
} | ||
|
||
toJSON(): string { | ||
return JSON.stringify(this.value); | ||
} | ||
|
||
toBuffer(): Uint8Array { | ||
return new TextEncoder().encode(this.toJSON()); | ||
} | ||
} |
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 |
---|---|---|
@@ -1,20 +1,37 @@ | ||
import { VmCallData, VmResult } from "../vm"; | ||
import type { VmCallData, VmResult } from "../vm"; | ||
import type { VmAction } from "./vm-actions"; | ||
|
||
export enum WorkerMessageType { | ||
VmCall = "VmCall", | ||
VmResult = "VmResult", | ||
VmActionResultBuffer = "VmActionResultBuffer", | ||
VmActionExecute = "VmActionExecute", | ||
} | ||
|
||
export interface VmCallWorkerMessage { | ||
processId: string, | ||
callData: VmCallData, | ||
notifierBuffer: SharedArrayBuffer, | ||
type: WorkerMessageType.VmCall, | ||
} | ||
|
||
export interface VmResultWorkerMessage { | ||
processId: string, | ||
result: VmResult, | ||
type: WorkerMessageType.VmResult, | ||
} | ||
|
||
export type WorkerMessage = VmCallWorkerMessage | VmResultWorkerMessage; | ||
export interface VmActionResultBufferMessage { | ||
buffer: SharedArrayBuffer, | ||
type: WorkerMessageType.VmActionResultBuffer, | ||
} | ||
|
||
export interface VmActionExecuteMessage { | ||
action: VmAction; | ||
type: WorkerMessageType.VmActionExecute; | ||
} | ||
|
||
|
||
export type WorkerMessage = | ||
| VmCallWorkerMessage | ||
| VmResultWorkerMessage | ||
| VmActionResultBufferMessage | ||
| VmActionExecuteMessage; |
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.