@superfaceai/one-sdk
Advanced tools
Comparing version 3.0.0-alpha.11 to 3.0.0-alpha.12
import { WASI } from '@cloudflare/workers-wasi'; | ||
import { App, HandleMap } from '../common/index.js'; | ||
import { App, HandleMap, UnexpectedError } from '../common/index.js'; | ||
export { PerformError, UnexpectedError } from '../common/error.js'; | ||
@@ -86,6 +86,30 @@ // @ts-ignore | ||
} | ||
class CfwTextStreamDecoder { | ||
constructor() { | ||
this.decoder = new TextDecoder(); | ||
this.decoderBuffer = ''; | ||
} | ||
/** Decodes streaming data and splits it by newline. */ | ||
decodeUtf8Lines(buffer) { | ||
this.decoderBuffer += this.decoder.decode(buffer, { stream: true }); | ||
return this.getLines(); | ||
} | ||
flush() { | ||
this.decoderBuffer += this.decoder.decode(); | ||
return this.getLines(); | ||
} | ||
getLines() { | ||
const lines = this.decoderBuffer.split('\n'); | ||
if (lines.length > 1) { | ||
this.decoderBuffer = lines[lines.length - 1]; | ||
return lines.slice(0, -1); | ||
} | ||
return []; | ||
} | ||
} | ||
class CfwWasiCompat { | ||
constructor(wasi) { | ||
this.stdoutDecoder = new CfwTextStreamDecoder(); | ||
this.stderrDecoder = new CfwTextStreamDecoder(); | ||
this.wasi = wasi; | ||
this.coder = new CfwTextCoder(); | ||
} | ||
@@ -119,4 +143,4 @@ get wasiImport() { | ||
fd_write(fd, ciovs_ptr, ciovs_len, retptr0) { | ||
// hijack stdout yolo | ||
if (fd !== 1) { | ||
// hijack stdout and stderr | ||
if (fd !== 1 && fd !== 2) { | ||
return this.wasi.wasiImport.fd_write(fd, ciovs_ptr, ciovs_len, retptr0); | ||
@@ -136,4 +160,9 @@ } | ||
view.setUint32(retptr0, writeCount, true); | ||
console.log(this.coder.decodeUtf8(buffer).trimEnd()); | ||
return 0; | ||
if (fd === 1) { | ||
this.stdoutDecoder.decodeUtf8Lines(buffer).forEach(line => console.log(line)); | ||
} | ||
else { | ||
this.stderrDecoder.decodeUtf8Lines(buffer).forEach(line => console.error(line)); | ||
} | ||
return 0; // SUCCESS | ||
} | ||
@@ -170,3 +199,3 @@ } | ||
for (const event of events) { | ||
console.error(event); | ||
console.error(event.trim()); | ||
} | ||
@@ -188,2 +217,3 @@ } | ||
async init() { | ||
// TODO: probably will need a lock like node platform has at some point | ||
if (this.ready) { | ||
@@ -207,3 +237,11 @@ return; | ||
const assetsPath = this.options.assetsPath ?? 'superface'; // TODO: path join? - not sure if we are going to stick with this VFS | ||
return await this.app.perform(`file://${assetsPath}/${resolvedProfile}.profile`, `file://${assetsPath}/${provider}.provider.json`, `file://${assetsPath}/${resolvedProfile}.${provider}.map.js`, usecase, input, parameters, security); | ||
try { | ||
return await this.app.perform(`file://${assetsPath}/${resolvedProfile}.profile`, `file://${assetsPath}/${provider}.provider.json`, `file://${assetsPath}/${resolvedProfile}.${provider}.map.js`, usecase, input, parameters, security); | ||
} | ||
catch (err) { | ||
if (err instanceof UnexpectedError && (err.name === 'WebAssemblyRuntimeError')) { | ||
await this.destroy(); | ||
} | ||
throw err; | ||
} | ||
} | ||
@@ -210,0 +248,0 @@ async sendMetrics() { |
{ | ||
"name": "@superfaceai/one-sdk", | ||
"version": "3.0.0-alpha.11", | ||
"version": "3.0.0-alpha.12", | ||
"main": "node/index.js", | ||
@@ -5,0 +5,0 @@ "type": "module", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
3768538
1903