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

@libsql/hrana-client

Package Overview
Dependencies
Maintainers
3
Versions
41
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@libsql/hrana-client - npm Package Compare versions

Comparing version 0.1.1 to 0.1.2

lib-cjs/errors.js

43

lib-cjs/convert.js
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.errorFromProto = exports.RowArray = exports.rowFromProto = exports.rowArrayFromProto = exports.stmtResultFromProto = exports.valueFromProto = exports.valueToProto = exports.stmtToProto = void 0;
function stmtToProto(stmtLike, wantRows) {
const errors_js_1 = require("./errors.js");
function stmtToProto(stmt, wantRows) {
let sql;
let args = [];
if (typeof stmtLike === "string") {
sql = stmtLike;
let namedArgs = [];
if (typeof stmt === "string") {
sql = stmt;
}
else {
sql = stmtLike[0];
args = stmtLike[1].map(valueToProto);
sql = stmt[0];
if (Array.isArray(stmt[1])) {
args = stmt[1].map(valueToProto);
}
else {
namedArgs = Object.entries(stmt[1]).map((entry) => {
const [key, value] = entry;
return { "name": key, "value": valueToProto(value) };
});
}
}
return { "sql": sql, "args": args, "want_rows": wantRows };
return { "sql": sql, "args": args, "named_args": namedArgs, "want_rows": wantRows };
}

@@ -25,3 +35,3 @@ exports.stmtToProto = stmtToProto;

else if (value instanceof ArrayBuffer) {
throw new Error("ArrayBuffer is not yet supported");
throw new errors_js_1.ClientError("ArrayBuffer is not yet supported");
}

@@ -47,6 +57,6 @@ else {

else if (value["type"] === "blob") {
throw new Error("blob is not yet supported");
throw new errors_js_1.ClientError("blob is not yet supported");
}
else {
throw new Error("Unexpected value type");
throw new errors_js_1.ProtoError("Unexpected value type");
}

@@ -56,7 +66,10 @@ }

function stmtResultFromProto(result) {
return { rowsAffected: result["affected_row_count"] };
return {
rowsAffected: result["affected_row_count"],
columnNames: result["cols"].map(col => col.name),
};
}
exports.stmtResultFromProto = stmtResultFromProto;
function rowArrayFromProto(result) {
const array = new RowArray(result["affected_row_count"]);
const array = new RowArray(stmtResultFromProto(result));
for (const row of result["rows"]) {

@@ -84,5 +97,7 @@ array.push(rowFromProto(result, row));

rowsAffected;
constructor(rowsAffected) {
columnNames;
constructor(result) {
super();
this.rowsAffected = rowsAffected;
this.rowsAffected = result.rowsAffected;
this.columnNames = result.columnNames;
Object.setPrototypeOf(this, RowArray.prototype);

@@ -93,4 +108,4 @@ }

function errorFromProto(error) {
return new Error(`Server returned error ${JSON.stringify(error["message"])}`);
return new errors_js_1.ResponseError(error["message"], error);
}
exports.errorFromProto = errorFromProto;
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __exportStar = (this && this.__exportStar) || function(m, exports) {
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
};
var __importDefault = (this && this.__importDefault) || function (mod) {

@@ -9,3 +23,5 @@ return (mod && mod.__esModule) ? mod : { "default": mod };

const convert_js_1 = require("./convert.js");
const errors_js_1 = require("./errors.js");
const id_alloc_js_1 = __importDefault(require("./id_alloc.js"));
__exportStar(require("./errors.js"), exports);
/** Open a Hrana client connected to the given `url`. */

@@ -52,3 +68,3 @@ function open(url, jwt) {

if (this.#closed !== undefined) {
throw new Error("Internal error: trying to send a message on a closed client");
throw new errors_js_1.ClientError("Internal error: trying to send a message on a closed client");
}

@@ -82,7 +98,7 @@ if (this.#socket.readyState >= isomorphic_ws_1.default.OPEN) {

const message = eventMessage ?? "Connection was closed due to an error";
this.#setClosed(new Error(message));
this.#setClosed(new errors_js_1.ClientError(message));
}
// The socket was closed.
#onSocketClose(event) {
this.#setClosed(new Error(`WebSocket was closed with code ${event.code}: ${event.reason}`));
this.#setClosed(new errors_js_1.ClientError(`WebSocket was closed with code ${event.code}: ${event.reason}`));
}

@@ -106,3 +122,3 @@ // Close the client with the given error.

this.#socket.close(3003, "Only string messages are accepted");
this.#setClosed(new Error("Received non-string message from server"));
this.#setClosed(new errors_js_1.ProtoError("Received non-string message from server"));
return;

@@ -123,3 +139,3 @@ }

if (this.#recvdHello) {
throw new Error("Received a duplicated hello response");
throw new errors_js_1.ProtoError("Received a duplicated hello response");
}

@@ -133,3 +149,3 @@ this.#recvdHello = true;

else if (!this.#recvdHello) {
throw new Error("Received a non-hello message before a hello response");
throw new errors_js_1.ProtoError("Received a non-hello message before a hello response");
}

@@ -141,6 +157,6 @@ if (msg["type"] === "response_ok") {

if (responseState === undefined) {
throw new Error("Received unexpected OK response");
throw new errors_js_1.ProtoError("Received unexpected OK response");
}
else if (responseState.type !== msg["response"]["type"]) {
throw new Error("Received unexpected type of response");
throw new errors_js_1.ProtoError("Received unexpected type of response");
}

@@ -160,3 +176,3 @@ try {

if (responseState === undefined) {
throw new Error("Received unexpected error response");
throw new errors_js_1.ProtoError("Received unexpected error response");
}

@@ -166,3 +182,3 @@ responseState.errorCallback((0, convert_js_1.errorFromProto)(msg["error"]));

else {
throw new Error("Received unexpected message type");
throw new errors_js_1.ProtoError("Received unexpected message type");
}

@@ -173,3 +189,3 @@ }

if (this.#closed !== undefined) {
throw new Error("Client is closed", { cause: this.#closed });
throw new errors_js_1.ClosedError("Client is closed", this.#closed);
}

@@ -217,7 +233,7 @@ const streamId = this.#streamIdAlloc.alloc();

if (streamState.closed !== undefined) {
errorCallback(new Error("Stream was closed", { cause: streamState.closed }));
errorCallback(new errors_js_1.ClosedError("Stream was closed", streamState.closed));
return;
}
else if (this.#closed !== undefined) {
errorCallback(new Error("Client was closed", { cause: this.#closed }));
errorCallback(new errors_js_1.ClosedError("Client was closed", this.#closed));
return;

@@ -234,3 +250,3 @@ }

close() {
this.#setClosed(new Error("Client was manually closed"));
this.#setClosed(new errors_js_1.ClientError("Client was manually closed"));
}

@@ -312,5 +328,5 @@ }

close() {
this.#client._closeStream(this.#state, new Error("Stream was manually closed"));
this.#client._closeStream(this.#state, new errors_js_1.ClientError("Stream was manually closed"));
}
}
exports.Stream = Stream;
import type * as proto from "./proto.js";
import { ResponseError } from "./errors.js";
/** A statement that you can send to the database. Either a plain SQL string, or an SQL string together with
* values for the `?` parameters.
*/
export type Stmt = string | [string, Array<Value>];
export declare function stmtToProto(stmtLike: Stmt, wantRows: boolean): proto.Stmt;
export type Stmt = string | [string, StmtArgs];
/** Arguments for a statement. Either an array that is bound to parameters by position, or an object with
* values that are bound to parameters by name. */
export type StmtArgs = Array<Value> | Record<string, Value>;
export declare function stmtToProto(stmt: Stmt, wantRows: boolean): proto.Stmt;
/** JavaScript values that you can get from the database. */

@@ -16,8 +20,10 @@ export type Value = null | string | number | ArrayBuffer;

rowsAffected: number;
columnNames: Array<string | null>;
}
export declare class RowArray extends Array<Row> implements StmtResult {
rowsAffected: number;
constructor(rowsAffected: number);
columnNames: Array<string | null>;
constructor(result: StmtResult);
}
export type Row = any;
export declare function errorFromProto(error: proto.Error): Error;
export declare function errorFromProto(error: proto.Error): ResponseError;

@@ -1,12 +0,22 @@

export function stmtToProto(stmtLike, wantRows) {
import { ClientError, ProtoError, ResponseError } from "./errors.js";
export function stmtToProto(stmt, wantRows) {
let sql;
let args = [];
if (typeof stmtLike === "string") {
sql = stmtLike;
let namedArgs = [];
if (typeof stmt === "string") {
sql = stmt;
}
else {
sql = stmtLike[0];
args = stmtLike[1].map(valueToProto);
sql = stmt[0];
if (Array.isArray(stmt[1])) {
args = stmt[1].map(valueToProto);
}
else {
namedArgs = Object.entries(stmt[1]).map((entry) => {
const [key, value] = entry;
return { "name": key, "value": valueToProto(value) };
});
}
}
return { "sql": sql, "args": args, "want_rows": wantRows };
return { "sql": sql, "args": args, "named_args": namedArgs, "want_rows": wantRows };
}

@@ -21,3 +31,3 @@ export function valueToProto(value) {

else if (value instanceof ArrayBuffer) {
throw new Error("ArrayBuffer is not yet supported");
throw new ClientError("ArrayBuffer is not yet supported");
}

@@ -42,13 +52,16 @@ else {

else if (value["type"] === "blob") {
throw new Error("blob is not yet supported");
throw new ClientError("blob is not yet supported");
}
else {
throw new Error("Unexpected value type");
throw new ProtoError("Unexpected value type");
}
}
export function stmtResultFromProto(result) {
return { rowsAffected: result["affected_row_count"] };
return {
rowsAffected: result["affected_row_count"],
columnNames: result["cols"].map(col => col.name),
};
}
export function rowArrayFromProto(result) {
const array = new RowArray(result["affected_row_count"]);
const array = new RowArray(stmtResultFromProto(result));
for (const row of result["rows"]) {

@@ -74,5 +87,7 @@ array.push(rowFromProto(result, row));

rowsAffected;
constructor(rowsAffected) {
columnNames;
constructor(result) {
super();
this.rowsAffected = rowsAffected;
this.rowsAffected = result.rowsAffected;
this.columnNames = result.columnNames;
Object.setPrototypeOf(this, RowArray.prototype);

@@ -82,3 +97,3 @@ }

export function errorFromProto(error) {
return new Error(`Server returned error ${JSON.stringify(error["message"])}`);
return new ResponseError(error["message"], error);
}

@@ -5,3 +5,4 @@ /// <reference types="ws" />

import type * as proto from "./proto.js";
export type { Stmt, Value, StmtResult, RowArray, Row } from "./convert";
export type { Stmt, StmtArgs, Value, StmtResult, RowArray, Row } from "./convert.js";
export * from "./errors.js";
export type { proto };

@@ -8,0 +9,0 @@ /** Open a Hrana client connected to the given `url`. */

import WebSocket from "isomorphic-ws";
import { stmtToProto, rowArrayFromProto, rowFromProto, stmtResultFromProto, valueFromProto, errorFromProto, } from "./convert.js";
import { ClientError, ProtoError, ClosedError } from "./errors.js";
import IdAlloc from "./id_alloc.js";
export * from "./errors.js";
/** Open a Hrana client connected to the given `url`. */

@@ -44,3 +46,3 @@ export function open(url, jwt) {

if (this.#closed !== undefined) {
throw new Error("Internal error: trying to send a message on a closed client");
throw new ClientError("Internal error: trying to send a message on a closed client");
}

@@ -74,7 +76,7 @@ if (this.#socket.readyState >= WebSocket.OPEN) {

const message = eventMessage ?? "Connection was closed due to an error";
this.#setClosed(new Error(message));
this.#setClosed(new ClientError(message));
}
// The socket was closed.
#onSocketClose(event) {
this.#setClosed(new Error(`WebSocket was closed with code ${event.code}: ${event.reason}`));
this.#setClosed(new ClientError(`WebSocket was closed with code ${event.code}: ${event.reason}`));
}

@@ -98,3 +100,3 @@ // Close the client with the given error.

this.#socket.close(3003, "Only string messages are accepted");
this.#setClosed(new Error("Received non-string message from server"));
this.#setClosed(new ProtoError("Received non-string message from server"));
return;

@@ -115,3 +117,3 @@ }

if (this.#recvdHello) {
throw new Error("Received a duplicated hello response");
throw new ProtoError("Received a duplicated hello response");
}

@@ -125,3 +127,3 @@ this.#recvdHello = true;

else if (!this.#recvdHello) {
throw new Error("Received a non-hello message before a hello response");
throw new ProtoError("Received a non-hello message before a hello response");
}

@@ -133,6 +135,6 @@ if (msg["type"] === "response_ok") {

if (responseState === undefined) {
throw new Error("Received unexpected OK response");
throw new ProtoError("Received unexpected OK response");
}
else if (responseState.type !== msg["response"]["type"]) {
throw new Error("Received unexpected type of response");
throw new ProtoError("Received unexpected type of response");
}

@@ -152,3 +154,3 @@ try {

if (responseState === undefined) {
throw new Error("Received unexpected error response");
throw new ProtoError("Received unexpected error response");
}

@@ -158,3 +160,3 @@ responseState.errorCallback(errorFromProto(msg["error"]));

else {
throw new Error("Received unexpected message type");
throw new ProtoError("Received unexpected message type");
}

@@ -165,3 +167,3 @@ }

if (this.#closed !== undefined) {
throw new Error("Client is closed", { cause: this.#closed });
throw new ClosedError("Client is closed", this.#closed);
}

@@ -209,7 +211,7 @@ const streamId = this.#streamIdAlloc.alloc();

if (streamState.closed !== undefined) {
errorCallback(new Error("Stream was closed", { cause: streamState.closed }));
errorCallback(new ClosedError("Stream was closed", streamState.closed));
return;
}
else if (this.#closed !== undefined) {
errorCallback(new Error("Client was closed", { cause: this.#closed }));
errorCallback(new ClosedError("Client was closed", this.#closed));
return;

@@ -226,3 +228,3 @@ }

close() {
this.#setClosed(new Error("Client was manually closed"));
this.#setClosed(new ClientError("Client was manually closed"));
}

@@ -303,4 +305,4 @@ }

close() {
this.#client._closeStream(this.#state, new Error("Stream was manually closed"));
this.#client._closeStream(this.#state, new ClientError("Stream was manually closed"));
}
}

@@ -60,5 +60,10 @@ export type int32 = number;

"sql": string;
"args": Array<Value>;
"args"?: Array<Value>;
"named_args"?: Array<NamedArg>;
"want_rows": boolean;
};
export type NamedArg = {
"name": string;
"value": Value;
};
export type StmtResult = {

@@ -65,0 +70,0 @@ "cols": Array<Col>;

{
"name": "@libsql/hrana-client",
"version": "0.1.1",
"version": "0.1.2",
"keywords": [

@@ -5,0 +5,0 @@ "hrana",

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