Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@superfaceai/one-sdk

Package Overview
Dependencies
Maintainers
3
Versions
135
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@superfaceai/one-sdk - npm Package Compare versions

Comparing version 3.0.0-alpha.4 to 3.0.0-alpha.5

common/error.d.ts

2

cloudflare/index.d.ts
import type { SecurityValuesMap } from '../common/index.js';
export { PerformError, UnexpectedError } from '../common/error.js';
export type ClientOptions = {

@@ -45,2 +46,1 @@ env?: Record<string, string>;

}
export {};
import { WASI } from '@cloudflare/workers-wasi';
import { App, HandleMap } from '../common/index.js';
export { PerformError, UnexpectedError } from '../common/error.js';
// @ts-ignore

@@ -4,0 +5,0 @@ import coreModule from '../assets/core-async.wasm';

@@ -40,2 +40,93 @@ import { SecurityValuesMap } from './security.js';

}
export declare class WasiError extends Error {
readonly errno: WasiErrno;
constructor(errno: WasiErrno);
}
export declare enum WasiErrno {
SUCCESS = 0,
TOOBIG = 1,
ACCES = 2,
ADDRINUSE = 3,
ADDRNOTAVAIL = 4,
AFNOSUPPORT = 5,
AGAIN = 6,
ALREADY = 7,
BADF = 8,
BADMSG = 9,
BUSY = 10,
CANCELED = 11,
CHILD = 12,
CONNABORTED = 13,
CONNREFUSED = 14,
CONNRESET = 15,
DEADLK = 16,
DESTADDRREQ = 17,
DOM = 18,
DQUOT = 19,
EXIST = 20,
FAULT = 21,
FBIG = 22,
HOSTUNREACH = 23,
IDRM = 24,
ILSEQ = 25,
INPROGRESS = 26,
INTR = 27,
INVAL = 28,
IO = 29,
ISCONN = 30,
ISDIR = 31,
LOOP = 32,
MFILE = 33,
MLINK = 34,
MSGSIZE = 35,
MULTIHOP = 36,
NAMETOOLONG = 37,
NETDOWN = 38,
NETRESET = 39,
NETUNREACH = 40,
NFILE = 41,
NOBUFS = 42,
NODEV = 43,
NOENT = 44,
NOEXEC = 45,
NOLCK = 46,
NOLINK = 47,
NOMEM = 48,
NOMSG = 49,
NOPROTOOPT = 50,
NOSPC = 51,
NOSYS = 52,
NOTCONN = 53,
NOTDIR = 54,
NOTEMPTY = 55,
NOTRECOVERABLE = 56,
NOTSOCK = 57,
NOTSUP = 58,
NOTTY = 59,
NXIO = 60,
OVERFLOW = 61,
OWNERDEAD = 62,
PERM = 63,
PIPE = 64,
PROTO = 65,
PROTONOSUPPORT = 66,
PROTOTYPE = 67,
RANGE = 68,
ROFS = 69,
SPIPE = 70,
SRCH = 71,
STALE = 72,
TIMEDOUT = 73,
TXTBSY = 74,
XDEV = 75,
NOTCAPABLE = 76
}
export declare class HostError extends Error {
readonly code: ErrorCode;
constructor(code: ErrorCode, message: string);
}
export declare enum ErrorCode {
NetworkError = "network:error",
NetworkInvalidUrl = "network:invalid_url"
}
export declare class App implements AppContext {

@@ -68,2 +159,5 @@ private readonly wasi;

teardown(): Promise<void>;
/**
* @throws {PerformError | UnexpectedError}
*/
perform(profileUrl: string, providerUrl: string, mapUrl: string, usecase: string, input: unknown, parameters: Record<string, string>, security: SecurityValuesMap): Promise<unknown>;

@@ -70,0 +164,0 @@ handleMessage(message: any): Promise<any>;

import { HandleMap } from './handle_map.js';
import { Asyncify } from './asyncify.js';
import * as sf_host from './sf_host.js';
import { PerformError, UnexpectedError } from './error.js';
export class WasiError extends Error {
constructor(errno) {
super(`WASI error: ${WasiErrno[errno]}`);
this.errno = errno;
}
}
export var WasiErrno;
(function (WasiErrno) {
WasiErrno[WasiErrno["SUCCESS"] = 0] = "SUCCESS";
WasiErrno[WasiErrno["TOOBIG"] = 1] = "TOOBIG";
WasiErrno[WasiErrno["ACCES"] = 2] = "ACCES";
WasiErrno[WasiErrno["ADDRINUSE"] = 3] = "ADDRINUSE";
WasiErrno[WasiErrno["ADDRNOTAVAIL"] = 4] = "ADDRNOTAVAIL";
WasiErrno[WasiErrno["AFNOSUPPORT"] = 5] = "AFNOSUPPORT";
WasiErrno[WasiErrno["AGAIN"] = 6] = "AGAIN";
WasiErrno[WasiErrno["ALREADY"] = 7] = "ALREADY";
WasiErrno[WasiErrno["BADF"] = 8] = "BADF";
WasiErrno[WasiErrno["BADMSG"] = 9] = "BADMSG";
WasiErrno[WasiErrno["BUSY"] = 10] = "BUSY";
WasiErrno[WasiErrno["CANCELED"] = 11] = "CANCELED";
WasiErrno[WasiErrno["CHILD"] = 12] = "CHILD";
WasiErrno[WasiErrno["CONNABORTED"] = 13] = "CONNABORTED";
WasiErrno[WasiErrno["CONNREFUSED"] = 14] = "CONNREFUSED";
WasiErrno[WasiErrno["CONNRESET"] = 15] = "CONNRESET";
WasiErrno[WasiErrno["DEADLK"] = 16] = "DEADLK";
WasiErrno[WasiErrno["DESTADDRREQ"] = 17] = "DESTADDRREQ";
WasiErrno[WasiErrno["DOM"] = 18] = "DOM";
WasiErrno[WasiErrno["DQUOT"] = 19] = "DQUOT";
WasiErrno[WasiErrno["EXIST"] = 20] = "EXIST";
WasiErrno[WasiErrno["FAULT"] = 21] = "FAULT";
WasiErrno[WasiErrno["FBIG"] = 22] = "FBIG";
WasiErrno[WasiErrno["HOSTUNREACH"] = 23] = "HOSTUNREACH";
WasiErrno[WasiErrno["IDRM"] = 24] = "IDRM";
WasiErrno[WasiErrno["ILSEQ"] = 25] = "ILSEQ";
WasiErrno[WasiErrno["INPROGRESS"] = 26] = "INPROGRESS";
WasiErrno[WasiErrno["INTR"] = 27] = "INTR";
WasiErrno[WasiErrno["INVAL"] = 28] = "INVAL";
WasiErrno[WasiErrno["IO"] = 29] = "IO";
WasiErrno[WasiErrno["ISCONN"] = 30] = "ISCONN";
WasiErrno[WasiErrno["ISDIR"] = 31] = "ISDIR";
WasiErrno[WasiErrno["LOOP"] = 32] = "LOOP";
WasiErrno[WasiErrno["MFILE"] = 33] = "MFILE";
WasiErrno[WasiErrno["MLINK"] = 34] = "MLINK";
WasiErrno[WasiErrno["MSGSIZE"] = 35] = "MSGSIZE";
WasiErrno[WasiErrno["MULTIHOP"] = 36] = "MULTIHOP";
WasiErrno[WasiErrno["NAMETOOLONG"] = 37] = "NAMETOOLONG";
WasiErrno[WasiErrno["NETDOWN"] = 38] = "NETDOWN";
WasiErrno[WasiErrno["NETRESET"] = 39] = "NETRESET";
WasiErrno[WasiErrno["NETUNREACH"] = 40] = "NETUNREACH";
WasiErrno[WasiErrno["NFILE"] = 41] = "NFILE";
WasiErrno[WasiErrno["NOBUFS"] = 42] = "NOBUFS";
WasiErrno[WasiErrno["NODEV"] = 43] = "NODEV";
WasiErrno[WasiErrno["NOENT"] = 44] = "NOENT";
WasiErrno[WasiErrno["NOEXEC"] = 45] = "NOEXEC";
WasiErrno[WasiErrno["NOLCK"] = 46] = "NOLCK";
WasiErrno[WasiErrno["NOLINK"] = 47] = "NOLINK";
WasiErrno[WasiErrno["NOMEM"] = 48] = "NOMEM";
WasiErrno[WasiErrno["NOMSG"] = 49] = "NOMSG";
WasiErrno[WasiErrno["NOPROTOOPT"] = 50] = "NOPROTOOPT";
WasiErrno[WasiErrno["NOSPC"] = 51] = "NOSPC";
WasiErrno[WasiErrno["NOSYS"] = 52] = "NOSYS";
WasiErrno[WasiErrno["NOTCONN"] = 53] = "NOTCONN";
WasiErrno[WasiErrno["NOTDIR"] = 54] = "NOTDIR";
WasiErrno[WasiErrno["NOTEMPTY"] = 55] = "NOTEMPTY";
WasiErrno[WasiErrno["NOTRECOVERABLE"] = 56] = "NOTRECOVERABLE";
WasiErrno[WasiErrno["NOTSOCK"] = 57] = "NOTSOCK";
WasiErrno[WasiErrno["NOTSUP"] = 58] = "NOTSUP";
WasiErrno[WasiErrno["NOTTY"] = 59] = "NOTTY";
WasiErrno[WasiErrno["NXIO"] = 60] = "NXIO";
WasiErrno[WasiErrno["OVERFLOW"] = 61] = "OVERFLOW";
WasiErrno[WasiErrno["OWNERDEAD"] = 62] = "OWNERDEAD";
WasiErrno[WasiErrno["PERM"] = 63] = "PERM";
WasiErrno[WasiErrno["PIPE"] = 64] = "PIPE";
WasiErrno[WasiErrno["PROTO"] = 65] = "PROTO";
WasiErrno[WasiErrno["PROTONOSUPPORT"] = 66] = "PROTONOSUPPORT";
WasiErrno[WasiErrno["PROTOTYPE"] = 67] = "PROTOTYPE";
WasiErrno[WasiErrno["RANGE"] = 68] = "RANGE";
WasiErrno[WasiErrno["ROFS"] = 69] = "ROFS";
WasiErrno[WasiErrno["SPIPE"] = 70] = "SPIPE";
WasiErrno[WasiErrno["SRCH"] = 71] = "SRCH";
WasiErrno[WasiErrno["STALE"] = 72] = "STALE";
WasiErrno[WasiErrno["TIMEDOUT"] = 73] = "TIMEDOUT";
WasiErrno[WasiErrno["TXTBSY"] = 74] = "TXTBSY";
WasiErrno[WasiErrno["XDEV"] = 75] = "XDEV";
WasiErrno[WasiErrno["NOTCAPABLE"] = 76] = "NOTCAPABLE";
})(WasiErrno || (WasiErrno = {}));
export class HostError extends Error {
constructor(code, message) {
super(message);
this.code = code;
this.name = code;
}
}
/// Core counterpart in core/host_to_core_std/src/unstable/mod.rs
export var ErrorCode;
(function (ErrorCode) {
ErrorCode["NetworkError"] = "network:error";
ErrorCode["NetworkInvalidUrl"] = "network:invalid_url";
})(ErrorCode || (ErrorCode = {}));
class ReadableStreamAdapter {

@@ -46,7 +146,9 @@ constructor(stream) {

* We can avoid this by synchronizing over core.
*
* Note that this is not thread safe (concurrent), but merely task safe (asynchronous).
*/
class AsyncMutex {
constructor(value) {
this.promise = Promise.resolve();
this.resolve = () => { };
this.condvar = Promise.resolve();
this.isLocked = false;
this.value = value;

@@ -63,7 +165,19 @@ }

async withLock(fn) {
await this.promise;
this.promise = new Promise((resolve) => { this.resolve = resolve; });
const result = await fn(this.value);
this.resolve();
return result;
do {
// Under the assumption that we do not have concurrency it can never happen that two tasks
// pass over the condition of this loop and think they both have a lock - that would imply there exists task preemption in synchronous code.
//
// If there ever is threading or task preemption, we will need to use other means (atomics, spinlocks).
await this.condvar;
} while (this.isLocked);
this.isLocked = true;
let notify;
this.condvar = new Promise((resolve) => { notify = resolve; });
try {
return await fn(this.value);
}
finally {
this.isLocked = false;
notify();
}
}

@@ -127,3 +241,3 @@ }

async setup() {
await this.core.withLock(core => core.setupFn());
return this.core.withLock(core => core.setupFn());
}

@@ -134,2 +248,5 @@ async teardown() {

}
/**
* @throws {PerformError | UnexpectedError}
*/
async perform(profileUrl, providerUrl, mapUrl, usecase, input, parameters, security) {

@@ -140,5 +257,18 @@ this.setSendMetricsTimeout();

await core.performFn();
const output = this.performState.output;
this.performState = undefined;
return output;
if (this.performState.result !== undefined) {
const result = this.performState.result;
this.performState = undefined;
return result;
}
if (this.performState.error !== undefined) {
const err = this.performState.error;
this.performState = undefined;
throw err;
}
if (this.performState.exception !== undefined) {
const exception = this.performState.exception;
this.performState = undefined;
throw exception;
}
throw new UnexpectedError('UnexpectedError', 'Unexpected perform state');
});

@@ -160,20 +290,31 @@ }

};
case 'perform-output':
this.performState.output = message.map_result;
case 'perform-output-result':
this.performState.result = message.result;
return { kind: 'ok' };
case 'perform-output-error':
this.performState.error = new PerformError(message.error);
return { kind: 'ok' };
case 'perform-output-exception':
this.performState.exception = new UnexpectedError(message.exception.error_code, message.exception.message);
return { kind: 'ok' };
case 'file-open': {
const handle = await this.fileSystem.open(message.path, {
createNew: message.create_new,
create: message.create,
truncate: message.truncate,
append: message.append,
write: message.write,
read: message.read
});
const res = this.streams.insert({
read: this.fileSystem.read.bind(this.fileSystem, handle),
write: this.fileSystem.write.bind(this.fileSystem, handle),
close: this.fileSystem.close.bind(this.fileSystem, handle)
});
return { kind: 'ok', stream: res };
try {
const handle = await this.fileSystem.open(message.path, {
createNew: message.create_new,
create: message.create,
truncate: message.truncate,
append: message.append,
write: message.write,
read: message.read
});
const res = this.streams.insert({
read: this.fileSystem.read.bind(this.fileSystem, handle),
write: this.fileSystem.write.bind(this.fileSystem, handle),
close: this.fileSystem.close.bind(this.fileSystem, handle)
});
return { kind: 'ok', stream: res };
}
catch (error) {
return { kind: 'err', errno: error.errno };
}
}

@@ -188,12 +329,22 @@ case 'http-call': {

}
const request = this.network.fetch(message.url, requestInit);
return { kind: 'ok', handle: this.requests.insert(request) };
try {
const request = this.network.fetch(message.url, requestInit);
return { kind: 'ok', handle: this.requests.insert(request) };
}
catch (error) {
return { kind: 'err', error_code: error.name, message: error.message };
}
}
case 'http-call-head': {
const response = await this.requests.remove(message.handle);
const bodyStream = new ReadableStreamAdapter(response.body); // TODO: handle when they are missing
return { kind: 'ok', status: response.status, headers: headersToMultimap(response.headers), body_stream: this.streams.insert(bodyStream) };
try {
const response = await this.requests.remove(message.handle);
const bodyStream = new ReadableStreamAdapter(response.body); // TODO: handle when they are missing
return { kind: 'ok', status: response.status, headers: headersToMultimap(response.headers), body_stream: this.streams.insert(bodyStream) };
}
catch (error) {
return { kind: 'err', error_code: error.name, message: error.message };
}
}
default:
return { 'kind': 'err', 'error': 'Unknown message' };
return { 'kind': 'err', 'error': `Unknown message ${message['kind']}` };
}

@@ -200,0 +351,0 @@ }

import type { SecurityValuesMap } from '../common/index.js';
import { Result } from './result.js';
import { PerformError, UnexpectedError } from '../common/error.js';
export type ClientOptions = {

@@ -21,5 +23,5 @@ assetsPath?: string;

private setup;
resolveProfileUrl(profile: string): string;
resolveMapUrl(profile: string, provider?: string): string;
resolveProviderUrl(provider: string): string;
resolveProfileUrl(profile: string): Promise<string>;
resolveMapUrl(profile: string, provider?: string): Promise<string>;
resolveProviderUrl(provider: string): Promise<string>;
private initProcessHooks;

@@ -48,4 +50,4 @@ private teardown;

constructor(internal: InternalClient, profile: Profile, name: string);
perform<TInput = unknown, TResult = unknown>(input: TInput | undefined, options: ClientPerformOptions): Promise<TResult>;
perform<TInput = unknown, TResult = unknown>(input: TInput | undefined, options: ClientPerformOptions): Promise<Result<TResult, PerformError | UnexpectedError>>;
}
export {};

@@ -6,2 +6,6 @@ import fs from 'fs/promises';

import { App, HandleMap } from '../common/index.js';
import { ErrorCode, HostError, WasiErrno } from '../common/app.js';
import { WasiError } from '../common/app.js';
import { err, ok } from './result.js';
import { PerformError, UnexpectedError } from '../common/error.js';
const CORE_PATH = createRequire(import.meta.url).resolve('../assets/core-async.wasm');

@@ -50,3 +54,3 @@ class NodeTextCoder {

if (file === undefined) {
throw new Error('invalid file handle - TODO: wasi error');
throw new WasiError(WasiErrno.BADF);
}

@@ -59,3 +63,3 @@ const result = await file.read(out, 0, out.byteLength);

if (file === undefined) {
throw new Error('invalid file handle - TODO: wasi error');
throw new WasiError(WasiErrno.BADF);
}

@@ -68,3 +72,3 @@ const result = await file.write(data, 0, data.length);

if (file === undefined) {
throw new Error('File does not exist');
throw new WasiError(WasiErrno.NOENT);
}

@@ -83,6 +87,22 @@ await file.close();

class NodeNetwork {
fetch(input, init) {
return fetch(input, init); // TODO: import from undici explicitly
// TODO: import from undici explicitly
async fetch(input, init) {
try {
return await fetch(input, init);
}
catch (err) {
throw fetchErrorToHostError(err);
}
}
}
function fetchErrorToHostError(error) {
if (error instanceof Error) {
let cause = '';
for (const [key, value] of Object.entries(error.cause ?? {})) {
cause += `${key}: ${value}\n`;
}
return new HostError(ErrorCode.NetworkError, `${error.name} ${error.message}${cause === '' ? '' : `\n${cause}`}`);
}
return new HostError(ErrorCode.NetworkError, 'Unknown error');
}
class InternalClient {

@@ -113,5 +133,5 @@ constructor(options = {}) {

await this.setup();
const profileUrl = this.resolveProfileUrl(profile);
const providerUrl = this.resolveProviderUrl(provider);
const mapUrl = this.resolveMapUrl(profile, provider);
const profileUrl = await this.resolveProfileUrl(profile);
const providerUrl = await this.resolveProviderUrl(provider);
const mapUrl = await this.resolveMapUrl(profile, provider);
return await this.app.perform(profileUrl, providerUrl, mapUrl, usecase, input, parameters, security);

@@ -127,23 +147,14 @@ }

}
resolveProfileUrl(profile) {
async resolveProfileUrl(profile) {
const resolvedProfile = profile.replace(/\//g, '.'); // TODO: be smarter about this
const path = resolve(this.assetsPath, `${resolvedProfile}.supr`);
if (fs.stat(path) === undefined) {
throw new Error(`Profile file does not exist, path: ${path}`);
}
return `file://${path}`;
}
resolveMapUrl(profile, provider) {
async resolveMapUrl(profile, provider) {
const resolvedProfile = profile.replace(/\//g, '.'); // TODO: be smarter about this
const path = resolve(this.assetsPath, `${resolvedProfile}.${provider}.js`);
if (fs.stat(path) === undefined) {
throw new Error(`Map file does not exist, path: ${path}`);
}
const path = resolve(this.assetsPath, `${resolvedProfile}.${provider}.suma.js`);
return `file://${path}`;
}
resolveProviderUrl(provider) {
async resolveProviderUrl(provider) {
const path = resolve(this.assetsPath, `${provider}.provider.json`);
if (fs.stat(path) === undefined) {
throw new Error(`Provider file does not exist, path: ${path}`);
}
return `file://${path}`;

@@ -180,3 +191,3 @@ }

static async loadLocal(internal, name) {
const profileUrl = internal.resolveProfileUrl(name);
const profileUrl = await internal.resolveProfileUrl(name);
return new Profile(internal, name, profileUrl);

@@ -195,5 +206,20 @@ }

async perform(input, options) {
return await this.internal.perform(this.profile.name, options.provider, this.name, input, options?.parameters, options?.security);
try {
const result = await this.internal.perform(this.profile.name, options.provider, this.name, input, options?.parameters, options?.security);
return ok(result);
}
catch (error) {
if (error instanceof PerformError) {
return err(error);
}
if (error instanceof UnexpectedError) {
return err(error);
}
if (error instanceof Error) {
return err(new UnexpectedError(error.name, error.message));
}
return err(new UnexpectedError('UnknownError', JSON.stringify(error)));
}
}
}
//# sourceMappingURL=index.js.map
{
"name": "@superfaceai/one-sdk",
"version": "3.0.0-alpha.4",
"version": "3.0.0-alpha.5",
"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

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc