Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@hyperbrowser/sdk",
"version": "1.5.0",
"version": "1.5.1",
"description": "Node SDK for Hyperbrowser API",
"author": "",
"repository": {
Expand Down
49 changes: 41 additions & 8 deletions src/customer-key.ts
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,46 @@ export const assertCustomerKey = (key: unknown, what = "customerKey"): CustomerK
return candidate;
};

/**
* @internal Checks that `keys` is a CustomerKey or a list of them before
* anything is sent. An empty list passes only with `allowEmpty`: an up-front
* check must not reject a call that never needs a key (an empty vault, or a
* rewrap with nothing to rewrap); `customerKeyFor` still rejects it when a
* key is actually needed.
*/
export const assertCustomerKeys = (
keys: unknown,
what = "customerKey",
{ allowEmpty = false }: { allowEmpty?: boolean } = {}
): readonly CustomerKey[] => {
if (!Array.isArray(keys)) {
return [assertCustomerKey(keys, what)];
}
if (keys.length === 0 && !allowEmpty) {
throw new HyperbrowserError(`Pass at least one customer key as ${what}`, {
code: "invalid_request",
retryable: false,
});
}
return keys.map((key, index) => assertCustomerKey(key, `${what}[${index}]`));
};

/** @internal Wraps `innerEnvelope` and checks that `wrap()` returned a non-empty string. */
export const wrapEnvelope = async (
customerKey: CustomerKey,
innerEnvelope: string,
encryptionContext: string
): Promise<string> => {
const envelope = await customerKey.wrap(innerEnvelope, encryptionContext);
if (typeof envelope !== "string" || !envelope) {
throw new HyperbrowserError("The customer key's wrap() must return the envelope as a string", {
code: "invalid_request",
retryable: false,
});
}
return envelope;
};

/**
* @internal Unwrap a wrapped version with the key among `keys` that wrapped
* it, and check the result is the inner envelope the version holds.
Expand All @@ -489,14 +529,7 @@ export const customerKeyFor = (
kid: string,
label: string
): CustomerKey => {
const list = (Array.isArray(keys) ? keys : [keys]) as readonly CustomerKey[];
if (list.length === 0) {
throw new HyperbrowserError("Pass at least one customer key", {
code: "invalid_request",
retryable: false,
});
}
list.forEach((key, index) => assertCustomerKey(key, `customerKey[${index}]`));
const list = assertCustomerKeys(keys);
const key = list.find((candidate) => candidate.kid === kid);
if (!key) {
throw new HyperbrowserError(
Expand Down
9 changes: 8 additions & 1 deletion src/services/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,14 @@ const getRequestId = (response: Response): string | undefined => {
const SERVER_ERROR_CODE_PATTERN = /^[A-Z][A-Z0-9]*(_[A-Z0-9]+)+$/;

/** Codes whose retryable status default (such as 503) would be wrong. */
const NON_RETRYABLE_ERROR_CODES = new Set(["SECRETS_NOT_CONFIGURED"]);
const NON_RETRYABLE_ERROR_CODES = new Set([
"SECRETS_ADMIN_REQUIRED",
"SECRETS_NOT_CONFIGURED",
"SECRET_KEY_UNAVAILABLE",
"SECRET_DECRYPT_FAILED",
"SECRET_ENVELOPE_INVALID",
"SECRET_VALUE_INVALID",
]);

/** The sandbox and secret an error is about, when the API names them. */
const errorMessageContext = (errorData: {
Expand Down
16 changes: 4 additions & 12 deletions src/services/sandboxes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,10 @@ export class SandboxHandle {
return (this.detail.exposedPorts ?? []).map((entry) => ({ ...entry }));
}

get startupSecrets(): SandboxDetail["startupSecrets"] {
return this.detail.startupSecrets?.map((entry) => ({ ...entry }));
}

toJSON(): SandboxDetail {
return { ...this.detail };
}
Expand Down Expand Up @@ -592,18 +596,6 @@ export class SandboxHandle {
return { ...this.runtimeSession };
}

private applyRuntimeSession(session: SandboxRuntimeState) {
this.runtimeSession = { ...session };
this.detail = {
...this.detail,
status: session.status,
region: session.region,
runtime: session.runtime,
token: session.token,
tokenExpiresAt: session.tokenExpiresAt,
};
}

private clearRuntimeSession(status: SandboxDetail["status"] = this.detail.status) {
this.runtimeSession = null;
this.detail = {
Expand Down
8 changes: 6 additions & 2 deletions src/services/secrets.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import {
assertCustomerKey,
assertCustomerKeys,
CustomerKey,
secretEnvelopeDigest,
unwrapVersion,
wrapEnvelope,
} from "../customer-key";
import { HyperbrowserError, invalidRequest } from "../error";
import { MAX_SECRET_VERSION, SECRET_NAME_PATTERN } from "../sandbox/secret-references";
Expand Down Expand Up @@ -280,6 +282,7 @@ export class SecretsService extends BaseService {
): Promise<UnwrappedSecretReference> {
const version = options?.version ?? "latest";
assertVersion(version, true);
assertCustomerKeys(customerKey);
try {
const resolved = await this.resolve(secret);
const wrapped = await this.fetchWrapped(resolved.id, version);
Expand All @@ -302,6 +305,7 @@ export class SecretsService extends BaseService {
*/
async rewrap(secret: string, params: RewrapSecretParams): Promise<WrappedSecretVersion[]> {
const { from, to, versions } = params ?? ({} as RewrapSecretParams);
assertCustomerKeys(from, "from", { allowEmpty: true });
assertCustomerKey(to, "to");
if (versions !== undefined) {
if (!Array.isArray(versions))
Expand All @@ -321,7 +325,7 @@ export class SecretsService extends BaseService {
const wrapped = await this.fetchWrapped(detail.id, version);
const label = `Version ${version} of secret ${detail.name}`;
const inner = await unwrapVersion(wrapped, from, label);
const envelope = await to.wrap(inner, detail.encryptionContext);
const envelope = await wrapEnvelope(to, inner, detail.encryptionContext);
rewrapped.push(
await this.putWrapping(detail.id, version, { envelope, innerDigest: wrapped.innerDigest })
);
Expand Down Expand Up @@ -417,7 +421,7 @@ export class SecretsService extends BaseService {
customerKey === undefined
? { envelope: inner }
: {
envelope: await customerKey.wrap(inner, secret.encryptionContext),
envelope: await wrapEnvelope(customerKey, inner, secret.encryptionContext),
innerDigest: secretEnvelopeDigest(inner),
innerKid: sealer.kid,
};
Expand Down
114 changes: 71 additions & 43 deletions src/services/sessions.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { promises as fs, Stats, createReadStream, ReadStream } from "fs";
import { promises as fs, constants as fsConstants, Stats, createReadStream, ReadStream } from "fs";
import * as path from "path";
import FormData from "form-data";
import { RequestInit } from "node-fetch";
Expand Down Expand Up @@ -261,13 +261,62 @@ export class SessionsService extends BaseService {
async uploadFile(id: string, fileOptions: UploadFileOptions): Promise<UploadFileResponse> {
const { fileInput, fileName } = fileOptions;

// Our own file stream, destroyed when the upload settles. A stream the
// caller passes in stays theirs and is never destroyed here.
let ownedStream: ReadStream | undefined;
let sourceStream: NodeJS.ReadableStream | undefined;
let streamError: HyperbrowserError | undefined;
let rejectStreamError: ((error: HyperbrowserError) => void) | undefined;
const streamFailure = new Promise<never>((_, reject) => {
rejectStreamError = reject;
});
streamFailure.catch(() => {});
// Aborts the in-flight request when the body fails, so the socket is not
// held open until the request timeout.
const controller = new AbortController();
let readLabel = "";

// form-data re-emits a source stream's "error" on the FormData, and
// node-fetch pipes the body without an error listener, so an unlistened
// FormData error is thrown as an uncaught exception. Every FormData body
// therefore gets a listener, whatever the input type.
const onStreamError = (error: Error) => {
if (!streamError) {
streamError = new HyperbrowserError(
`Failed to read file ${readLabel}: ${error.message}`,
undefined
);
}
controller.abort();
rejectStreamError?.(streamError);
};
const buildBody = (
file: NodeJS.ReadableStream | Buffer,
filename: string
): RequestInit => {
const formData = new FormData();
formData.on("error", onStreamError);
if (!Buffer.isBuffer(file)) {
sourceStream = file;
file.on("error", onStreamError);
}
formData.append("file", file, { filename });
return {
method: "POST",
body: formData,
headers: formData.getHeaders(),
signal: controller.signal,
};
};

try {
let fetchOptions: RequestInit;

if (typeof fileInput === "string") {
let stats: Stats;
try {
stats = await fs.stat(fileInput);
await fs.access(fileInput, fsConstants.R_OK);
} catch (error: any) {
if (error.code === "ENOENT") {
throw new HyperbrowserError(`File not found: ${fileInput}`, undefined);
Expand All @@ -288,72 +337,51 @@ export class SessionsService extends BaseService {
throw new HyperbrowserError(`Path is not a file: ${fileInput}`, undefined);
}

const formData = new FormData();
const fileStream = createReadStream(fileInput);
const fileBaseName = fileName || path.basename(fileInput);

fileStream.on("error", (error) => {
throw new HyperbrowserError(
`Failed to read file ${fileInput}: ${error.message}`,
undefined
);
});

formData.append("file", fileStream, {
filename: fileBaseName,
});

fetchOptions = {
method: "POST",
body: formData,
headers: formData.getHeaders(),
};
readLabel = `at ${fileInput}`;
ownedStream = createReadStream(fileInput);
fetchOptions = buildBody(ownedStream, fileName || path.basename(fileInput));
} else if (this.isReadableStream(fileInput)) {
const formData = new FormData();

let tmpFileName = fileName || `file-${Date.now()}`;
if (fileInput.path && typeof fileInput.path === "string" && !fileName) {
tmpFileName = path.basename(fileInput.path);
}

formData.append("file", fileInput, {
filename: tmpFileName,
});

fetchOptions = {
method: "POST",
body: formData,
headers: formData.getHeaders(),
};
readLabel = `stream ${tmpFileName}`;
fetchOptions = buildBody(fileInput, tmpFileName);
} else if (Buffer.isBuffer(fileInput)) {
if (!fileName) {
throw new HyperbrowserError("fileName is required when uploading Buffer data", undefined);
}

const formData = new FormData();
formData.append("file", fileInput, {
filename: fileName,
});

fetchOptions = {
method: "POST",
body: formData,
headers: formData.getHeaders(),
};
readLabel = `buffer ${fileName}`;
fetchOptions = buildBody(fileInput, fileName);
} else {
throw new HyperbrowserError(
"Unsupported file input type. Please provide a file path string, ReadStream, or Buffer.",
undefined
);
}

return await this.request<UploadFileResponse>(`/session/${id}/uploads`, fetchOptions);
const requestPromise = this.request<UploadFileResponse>(
`/session/${id}/uploads`,
fetchOptions
);
requestPromise.catch(() => {});
return await Promise.race([requestPromise, streamFailure]);
} catch (error) {
if (streamError) {
throw streamError;
}
if (error instanceof HyperbrowserError) {
throw error;
}

throw new HyperbrowserError(`Failed to upload file for session ${id}: ${error}`, undefined);
} finally {
if (sourceStream && sourceStream !== ownedStream) {
sourceStream.removeListener("error", onStreamError);
}
ownedStream?.destroy();
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/services/vaults.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CustomerKey, unwrapVersion } from "../customer-key";
import { assertCustomerKeys, CustomerKey, unwrapVersion } from "../customer-key";
import { HyperbrowserError, invalidRequest } from "../error";
import {
CreateVaultParams,
Expand Down Expand Up @@ -94,6 +94,7 @@ export class VaultsService extends BaseService {
vault: string,
customerKey: CustomerKey | readonly CustomerKey[]
): Promise<VaultReference> {
assertCustomerKeys(customerKey, "customerKey", { allowEmpty: true });
const wrapped = await this.getWrapped(vault);
const unwrapped: VaultReference["unwrapped"] = {};
for (const [env, entry] of Object.entries(wrapped.entries ?? {})) {
Expand Down
9 changes: 5 additions & 4 deletions src/tools/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,11 @@ export class WebsiteExtractTool {
static anthropicToolDefinition = EXTRACT_TOOL_ANTHROPIC;

static async runnable(hb: HyperbrowserClient, params: StartExtractJobParams): Promise<string> {
if (params.schema && typeof params.schema === "string") {
params.schema = JSON.parse(params.schema);
}
const resp = await hb.extract.startAndWait(params);
const requestParams: StartExtractJobParams =
params.schema && typeof params.schema === "string"
? { ...params, schema: JSON.parse(params.schema) }
: { ...params };
const resp = await hb.extract.startAndWait(requestParams);
return resp.data ? JSON.stringify(resp.data) : "";
}
}
Expand Down
Loading