@solana/rpc-transport
Advanced tools
Comparing version 0.0.0-experimental.2 to 2.0.0-experimental.3290268
@@ -1,7 +0,4 @@ | ||
import 'node-fetch'; | ||
// ../build-scripts/env-shim.ts | ||
var __DEV__ = /* @__PURE__ */ (() => process["env"].NODE_ENV === "development")(); | ||
// ../fetch-impl-browser/dist/index.browser.js | ||
var { fetch } = globalThis; | ||
var src_default = fetch; | ||
// src/http-request-errors.ts | ||
@@ -18,14 +15,72 @@ var SolanaHttpError = class extends Error { | ||
}; | ||
async function makeHttpRequest({ payload, url }) { | ||
// src/http-request-headers.ts | ||
var DISALLOWED_HEADERS = { | ||
accept: true, | ||
"content-length": true, | ||
"content-type": true | ||
}; | ||
var FORBIDDEN_HEADERS = { | ||
"accept-charset": true, | ||
"accept-encoding": true, | ||
"access-control-request-headers": true, | ||
"access-control-request-method": true, | ||
connection: true, | ||
"content-length": true, | ||
cookie: true, | ||
date: true, | ||
dnt: true, | ||
expect: true, | ||
host: true, | ||
"keep-alive": true, | ||
origin: true, | ||
"permissions-policy": true, | ||
// No currently available Typescript technique allows you to match on a prefix. | ||
// 'proxy-':true, | ||
// 'sec-':true, | ||
referer: true, | ||
te: true, | ||
trailer: true, | ||
"transfer-encoding": true, | ||
upgrade: true, | ||
via: true | ||
}; | ||
function assertIsAllowedHttpRequestHeaders(headers) { | ||
const badHeaders = Object.keys(headers).filter((headerName) => { | ||
const lowercaseHeaderName = headerName.toLowerCase(); | ||
return DISALLOWED_HEADERS[headerName.toLowerCase()] === true || FORBIDDEN_HEADERS[headerName.toLowerCase()] === true || lowercaseHeaderName.startsWith("proxy-") || lowercaseHeaderName.startsWith("sec-"); | ||
}); | ||
if (badHeaders.length > 0) { | ||
throw new Error( | ||
`${badHeaders.length > 1 ? "These headers are" : "This header is"} forbidden: \`${badHeaders.join("`, `")}\`. Learn more at https://developer.mozilla.org/en-US/docs/Glossary/Forbidden_header_name.` | ||
); | ||
} | ||
} | ||
function normalizeHeaders(headers) { | ||
const out = {}; | ||
for (const headerName in headers) { | ||
out[headerName.toLowerCase()] = headers[headerName]; | ||
} | ||
return out; | ||
} | ||
// ../fetch-impl/dist/index.browser.js | ||
var e = globalThis.fetch; | ||
// src/http-request.ts | ||
async function makeHttpRequest({ headers, payload, signal, url }) { | ||
const body = JSON.stringify(payload); | ||
const requestInfo = { | ||
body: JSON.stringify(payload), | ||
body, | ||
headers: { | ||
"Content-type": "application/json" | ||
...headers && normalizeHeaders(headers), | ||
// Keep these headers lowercase so they will override any user-supplied headers above. | ||
accept: "application/json", | ||
"content-length": body.length.toString(), | ||
"content-type": "application/json; charset=utf-8" | ||
}, | ||
method: "POST" | ||
method: "POST", | ||
signal | ||
}; | ||
let response; | ||
{ | ||
response = await src_default(url, requestInfo); | ||
} | ||
const response = await e(url, requestInfo); | ||
if (!response.ok) { | ||
@@ -40,3 +95,3 @@ throw new SolanaHttpError({ | ||
// src/json-rpc-errors.ts | ||
// src/json-rpc-transport/json-rpc-errors.ts | ||
var SolanaJsonRpcError = class extends Error { | ||
@@ -54,3 +109,3 @@ constructor(details) { | ||
// src/json-rpc-message-id.ts | ||
// src/json-rpc-transport/json-rpc-message-id.ts | ||
var _nextMessageId = 0; | ||
@@ -63,3 +118,3 @@ function getNextMessageId() { | ||
// src/json-rpc-message.ts | ||
// src/json-rpc-transport/json-rpc-message.ts | ||
function createJsonRpcMessage(method, params) { | ||
@@ -74,51 +129,48 @@ return { | ||
// src/params-patcher.ts | ||
function visitNode(value, keyPath, onIntegerOverflow) { | ||
if (Array.isArray(value)) { | ||
return value.map( | ||
(element, ii) => visitNode(element, [...keyPath, ii], onIntegerOverflow) | ||
); | ||
} else if (typeof value === "object" && value !== null) { | ||
const out = {}; | ||
for (const propName in value) { | ||
if (Object.prototype.hasOwnProperty.call(value, propName)) { | ||
out[propName] = visitNode(value[propName], [...keyPath, propName], onIntegerOverflow); | ||
// src/json-rpc-transport/index.ts | ||
function createArmedJsonRpcTransport(transportConfig, pendingRequest) { | ||
const overrides = { | ||
async send(options) { | ||
const { methodName, params, responseProcessor } = pendingRequest; | ||
const payload = createJsonRpcMessage(methodName, params); | ||
const response = await makeHttpRequest({ | ||
headers: transportConfig.headers, | ||
payload, | ||
signal: options?.abortSignal, | ||
url: transportConfig.url | ||
}); | ||
if ("error" in response) { | ||
throw new SolanaJsonRpcError(response.error); | ||
} else { | ||
return responseProcessor ? responseProcessor(response.result) : response.result; | ||
} | ||
} | ||
return out; | ||
} else if (typeof value === "bigint") { | ||
if (onIntegerOverflow && (value > Number.MAX_SAFE_INTEGER || value < -Number.MAX_SAFE_INTEGER)) { | ||
onIntegerOverflow(keyPath, value); | ||
} | ||
return Number(value); | ||
} else { | ||
return value; | ||
} | ||
} | ||
function patchParamsForSolanaLabsRpc(params, onIntegerOverflow) { | ||
return visitNode(params, [], onIntegerOverflow); | ||
} | ||
// src/json-rpc-transport.ts | ||
function createArmedJsonRpcTransport(transportConfig, pendingMessage) { | ||
const { url } = transportConfig; | ||
const transport = { | ||
async send() { | ||
return await sendPayload(pendingMessage, url); | ||
} | ||
}; | ||
return makeProxy(transport, transportConfig, pendingMessage); | ||
return makeProxy(transportConfig, overrides, pendingRequest); | ||
} | ||
function createArmedBatchJsonRpcTransport(transportConfig, pendingMessages) { | ||
const { url } = transportConfig; | ||
const transport = { | ||
async sendBatch() { | ||
return await sendPayload(pendingMessages, url); | ||
function createArmedBatchJsonRpcTransport(transportConfig, pendingRequests) { | ||
const overrides = { | ||
async sendBatch(options) { | ||
const payload = pendingRequests.map(({ methodName, params }) => createJsonRpcMessage(methodName, params)); | ||
const responses = await makeHttpRequest({ | ||
headers: transportConfig.headers, | ||
payload, | ||
signal: options?.abortSignal, | ||
url: transportConfig.url | ||
}); | ||
const requestOrder = payload.map((p) => p.id); | ||
return responses.sort((a, b) => requestOrder.indexOf(a.id) - requestOrder.indexOf(b.id)).map((response, ii) => { | ||
if ("error" in response) { | ||
throw new SolanaJsonRpcError(response.error); | ||
} else { | ||
const { responseProcessor } = pendingRequests[ii]; | ||
return responseProcessor ? responseProcessor(response.result) : response.result; | ||
} | ||
}); | ||
} | ||
}; | ||
return makeProxy(transport, transportConfig, pendingMessages); | ||
return makeProxy(transportConfig, overrides, pendingRequests); | ||
} | ||
function makeProxy(transport, transportConfig, pendingRequestOrRequests) { | ||
const { onIntegerOverflow } = transportConfig; | ||
return new Proxy(transport, { | ||
function makeProxy(transportConfig, overrides, pendingRequestOrRequests) { | ||
return new Proxy(transportConfig.api, { | ||
defineProperty() { | ||
@@ -130,18 +182,15 @@ return false; | ||
}, | ||
get(...args) { | ||
const [target, p] = args; | ||
return p in target ? Reflect.get(...args) : function(...params) { | ||
get(target, p, receiver) { | ||
if (overrides && Reflect.has(overrides, p)) { | ||
return Reflect.get(overrides, p, receiver); | ||
} | ||
return function(...rawParams) { | ||
const methodName = p.toString(); | ||
const patchedParams = patchParamsForSolanaLabsRpc( | ||
params, | ||
onIntegerOverflow ? (keyPath, value) => { | ||
onIntegerOverflow(methodName, keyPath, value); | ||
} : void 0 | ||
); | ||
const newMessage = createJsonRpcMessage(methodName, patchedParams); | ||
const createTransportRequest = Reflect.get(target, methodName, receiver); | ||
const newRequest = createTransportRequest ? createTransportRequest(...rawParams) : { methodName, params: rawParams }; | ||
if (pendingRequestOrRequests == null) { | ||
return createArmedJsonRpcTransport(transportConfig, newMessage); | ||
return createArmedJsonRpcTransport(transportConfig, newRequest); | ||
} else { | ||
const nextPendingMessages = Array.isArray(pendingRequestOrRequests) ? [...pendingRequestOrRequests, newMessage] : [pendingRequestOrRequests, newMessage]; | ||
return createArmedBatchJsonRpcTransport(transportConfig, nextPendingMessages); | ||
const nextPendingRequests = Array.isArray(pendingRequestOrRequests) ? [...pendingRequestOrRequests, newRequest] : [pendingRequestOrRequests, newRequest]; | ||
return createArmedBatchJsonRpcTransport(transportConfig, nextPendingRequests); | ||
} | ||
@@ -152,25 +201,8 @@ }; | ||
} | ||
function processResponse(response) { | ||
if ("error" in response) { | ||
throw new SolanaJsonRpcError(response.error); | ||
} else { | ||
return response.result; | ||
function createJsonRpcTransport(transportConfig) { | ||
if (__DEV__ && transportConfig.headers) { | ||
assertIsAllowedHttpRequestHeaders(transportConfig.headers); | ||
} | ||
return makeProxy(transportConfig); | ||
} | ||
async function sendPayload(payload, url) { | ||
const responseOrResponses = await makeHttpRequest({ | ||
payload, | ||
url | ||
}); | ||
if (Array.isArray(responseOrResponses)) { | ||
const requestOrder = payload.map((p) => p.id); | ||
return responseOrResponses.sort((a, b) => requestOrder.indexOf(a.id) - requestOrder.indexOf(b.id)).map(processResponse); | ||
} else { | ||
return processResponse(responseOrResponses); | ||
} | ||
} | ||
function createJsonRpcTransport(transportConfig) { | ||
const transport = {}; | ||
return makeProxy(transport, transportConfig); | ||
} | ||
@@ -177,0 +209,0 @@ export { createJsonRpcTransport }; |
@@ -1,7 +0,4 @@ | ||
import 'node-fetch'; | ||
// ../build-scripts/env-shim.ts | ||
var __DEV__ = /* @__PURE__ */ (() => process["env"].NODE_ENV === "development")(); | ||
// ../fetch-impl-browser/dist/index.browser.js | ||
var { fetch } = globalThis; | ||
var src_default = fetch; | ||
// src/http-request-errors.ts | ||
@@ -18,14 +15,72 @@ var SolanaHttpError = class extends Error { | ||
}; | ||
async function makeHttpRequest({ payload, url }) { | ||
// src/http-request-headers.ts | ||
var DISALLOWED_HEADERS = { | ||
accept: true, | ||
"content-length": true, | ||
"content-type": true | ||
}; | ||
var FORBIDDEN_HEADERS = { | ||
"accept-charset": true, | ||
"accept-encoding": true, | ||
"access-control-request-headers": true, | ||
"access-control-request-method": true, | ||
connection: true, | ||
"content-length": true, | ||
cookie: true, | ||
date: true, | ||
dnt: true, | ||
expect: true, | ||
host: true, | ||
"keep-alive": true, | ||
origin: true, | ||
"permissions-policy": true, | ||
// No currently available Typescript technique allows you to match on a prefix. | ||
// 'proxy-':true, | ||
// 'sec-':true, | ||
referer: true, | ||
te: true, | ||
trailer: true, | ||
"transfer-encoding": true, | ||
upgrade: true, | ||
via: true | ||
}; | ||
function assertIsAllowedHttpRequestHeaders(headers) { | ||
const badHeaders = Object.keys(headers).filter((headerName) => { | ||
const lowercaseHeaderName = headerName.toLowerCase(); | ||
return DISALLOWED_HEADERS[headerName.toLowerCase()] === true || FORBIDDEN_HEADERS[headerName.toLowerCase()] === true || lowercaseHeaderName.startsWith("proxy-") || lowercaseHeaderName.startsWith("sec-"); | ||
}); | ||
if (badHeaders.length > 0) { | ||
throw new Error( | ||
`${badHeaders.length > 1 ? "These headers are" : "This header is"} forbidden: \`${badHeaders.join("`, `")}\`. Learn more at https://developer.mozilla.org/en-US/docs/Glossary/Forbidden_header_name.` | ||
); | ||
} | ||
} | ||
function normalizeHeaders(headers) { | ||
const out = {}; | ||
for (const headerName in headers) { | ||
out[headerName.toLowerCase()] = headers[headerName]; | ||
} | ||
return out; | ||
} | ||
// ../fetch-impl/dist/index.browser.js | ||
var e = globalThis.fetch; | ||
// src/http-request.ts | ||
async function makeHttpRequest({ headers, payload, signal, url }) { | ||
const body = JSON.stringify(payload); | ||
const requestInfo = { | ||
body: JSON.stringify(payload), | ||
body, | ||
headers: { | ||
"Content-type": "application/json" | ||
...headers && normalizeHeaders(headers), | ||
// Keep these headers lowercase so they will override any user-supplied headers above. | ||
accept: "application/json", | ||
"content-length": body.length.toString(), | ||
"content-type": "application/json; charset=utf-8" | ||
}, | ||
method: "POST" | ||
method: "POST", | ||
signal | ||
}; | ||
let response; | ||
{ | ||
response = await src_default(url, requestInfo); | ||
} | ||
const response = await e(url, requestInfo); | ||
if (!response.ok) { | ||
@@ -40,3 +95,3 @@ throw new SolanaHttpError({ | ||
// src/json-rpc-errors.ts | ||
// src/json-rpc-transport/json-rpc-errors.ts | ||
var SolanaJsonRpcError = class extends Error { | ||
@@ -54,3 +109,3 @@ constructor(details) { | ||
// src/json-rpc-message-id.ts | ||
// src/json-rpc-transport/json-rpc-message-id.ts | ||
var _nextMessageId = 0; | ||
@@ -63,3 +118,3 @@ function getNextMessageId() { | ||
// src/json-rpc-message.ts | ||
// src/json-rpc-transport/json-rpc-message.ts | ||
function createJsonRpcMessage(method, params) { | ||
@@ -74,51 +129,48 @@ return { | ||
// src/params-patcher.ts | ||
function visitNode(value, keyPath, onIntegerOverflow) { | ||
if (Array.isArray(value)) { | ||
return value.map( | ||
(element, ii) => visitNode(element, [...keyPath, ii], onIntegerOverflow) | ||
); | ||
} else if (typeof value === "object" && value !== null) { | ||
const out = {}; | ||
for (const propName in value) { | ||
if (Object.prototype.hasOwnProperty.call(value, propName)) { | ||
out[propName] = visitNode(value[propName], [...keyPath, propName], onIntegerOverflow); | ||
// src/json-rpc-transport/index.ts | ||
function createArmedJsonRpcTransport(transportConfig, pendingRequest) { | ||
const overrides = { | ||
async send(options) { | ||
const { methodName, params, responseProcessor } = pendingRequest; | ||
const payload = createJsonRpcMessage(methodName, params); | ||
const response = await makeHttpRequest({ | ||
headers: transportConfig.headers, | ||
payload, | ||
signal: options?.abortSignal, | ||
url: transportConfig.url | ||
}); | ||
if ("error" in response) { | ||
throw new SolanaJsonRpcError(response.error); | ||
} else { | ||
return responseProcessor ? responseProcessor(response.result) : response.result; | ||
} | ||
} | ||
return out; | ||
} else if (typeof value === "bigint") { | ||
if (onIntegerOverflow && (value > Number.MAX_SAFE_INTEGER || value < -Number.MAX_SAFE_INTEGER)) { | ||
onIntegerOverflow(keyPath, value); | ||
} | ||
return Number(value); | ||
} else { | ||
return value; | ||
} | ||
} | ||
function patchParamsForSolanaLabsRpc(params, onIntegerOverflow) { | ||
return visitNode(params, [], onIntegerOverflow); | ||
} | ||
// src/json-rpc-transport.ts | ||
function createArmedJsonRpcTransport(transportConfig, pendingMessage) { | ||
const { url } = transportConfig; | ||
const transport = { | ||
async send() { | ||
return await sendPayload(pendingMessage, url); | ||
} | ||
}; | ||
return makeProxy(transport, transportConfig, pendingMessage); | ||
return makeProxy(transportConfig, overrides, pendingRequest); | ||
} | ||
function createArmedBatchJsonRpcTransport(transportConfig, pendingMessages) { | ||
const { url } = transportConfig; | ||
const transport = { | ||
async sendBatch() { | ||
return await sendPayload(pendingMessages, url); | ||
function createArmedBatchJsonRpcTransport(transportConfig, pendingRequests) { | ||
const overrides = { | ||
async sendBatch(options) { | ||
const payload = pendingRequests.map(({ methodName, params }) => createJsonRpcMessage(methodName, params)); | ||
const responses = await makeHttpRequest({ | ||
headers: transportConfig.headers, | ||
payload, | ||
signal: options?.abortSignal, | ||
url: transportConfig.url | ||
}); | ||
const requestOrder = payload.map((p) => p.id); | ||
return responses.sort((a, b) => requestOrder.indexOf(a.id) - requestOrder.indexOf(b.id)).map((response, ii) => { | ||
if ("error" in response) { | ||
throw new SolanaJsonRpcError(response.error); | ||
} else { | ||
const { responseProcessor } = pendingRequests[ii]; | ||
return responseProcessor ? responseProcessor(response.result) : response.result; | ||
} | ||
}); | ||
} | ||
}; | ||
return makeProxy(transport, transportConfig, pendingMessages); | ||
return makeProxy(transportConfig, overrides, pendingRequests); | ||
} | ||
function makeProxy(transport, transportConfig, pendingRequestOrRequests) { | ||
const { onIntegerOverflow } = transportConfig; | ||
return new Proxy(transport, { | ||
function makeProxy(transportConfig, overrides, pendingRequestOrRequests) { | ||
return new Proxy(transportConfig.api, { | ||
defineProperty() { | ||
@@ -130,18 +182,15 @@ return false; | ||
}, | ||
get(...args) { | ||
const [target, p] = args; | ||
return p in target ? Reflect.get(...args) : function(...params) { | ||
get(target, p, receiver) { | ||
if (overrides && Reflect.has(overrides, p)) { | ||
return Reflect.get(overrides, p, receiver); | ||
} | ||
return function(...rawParams) { | ||
const methodName = p.toString(); | ||
const patchedParams = patchParamsForSolanaLabsRpc( | ||
params, | ||
onIntegerOverflow ? (keyPath, value) => { | ||
onIntegerOverflow(methodName, keyPath, value); | ||
} : void 0 | ||
); | ||
const newMessage = createJsonRpcMessage(methodName, patchedParams); | ||
const createTransportRequest = Reflect.get(target, methodName, receiver); | ||
const newRequest = createTransportRequest ? createTransportRequest(...rawParams) : { methodName, params: rawParams }; | ||
if (pendingRequestOrRequests == null) { | ||
return createArmedJsonRpcTransport(transportConfig, newMessage); | ||
return createArmedJsonRpcTransport(transportConfig, newRequest); | ||
} else { | ||
const nextPendingMessages = Array.isArray(pendingRequestOrRequests) ? [...pendingRequestOrRequests, newMessage] : [pendingRequestOrRequests, newMessage]; | ||
return createArmedBatchJsonRpcTransport(transportConfig, nextPendingMessages); | ||
const nextPendingRequests = Array.isArray(pendingRequestOrRequests) ? [...pendingRequestOrRequests, newRequest] : [pendingRequestOrRequests, newRequest]; | ||
return createArmedBatchJsonRpcTransport(transportConfig, nextPendingRequests); | ||
} | ||
@@ -152,25 +201,8 @@ }; | ||
} | ||
function processResponse(response) { | ||
if ("error" in response) { | ||
throw new SolanaJsonRpcError(response.error); | ||
} else { | ||
return response.result; | ||
function createJsonRpcTransport(transportConfig) { | ||
if (__DEV__ && transportConfig.headers) { | ||
assertIsAllowedHttpRequestHeaders(transportConfig.headers); | ||
} | ||
return makeProxy(transportConfig); | ||
} | ||
async function sendPayload(payload, url) { | ||
const responseOrResponses = await makeHttpRequest({ | ||
payload, | ||
url | ||
}); | ||
if (Array.isArray(responseOrResponses)) { | ||
const requestOrder = payload.map((p) => p.id); | ||
return responseOrResponses.sort((a, b) => requestOrder.indexOf(a.id) - requestOrder.indexOf(b.id)).map(processResponse); | ||
} else { | ||
return processResponse(responseOrResponses); | ||
} | ||
} | ||
function createJsonRpcTransport(transportConfig) { | ||
const transport = {}; | ||
return makeProxy(transport, transportConfig); | ||
} | ||
@@ -177,0 +209,0 @@ export { createJsonRpcTransport }; |
@@ -1,3 +0,6 @@ | ||
import fetchImplNode from 'node-fetch'; | ||
import t from 'node-fetch'; | ||
// ../build-scripts/env-shim.ts | ||
var __DEV__ = /* @__PURE__ */ (() => process["env"].NODE_ENV === "development")(); | ||
// src/http-request-errors.ts | ||
@@ -14,14 +17,70 @@ var SolanaHttpError = class extends Error { | ||
}; | ||
async function makeHttpRequest({ payload, url }) { | ||
// src/http-request-headers.ts | ||
var DISALLOWED_HEADERS = { | ||
accept: true, | ||
"content-length": true, | ||
"content-type": true | ||
}; | ||
var FORBIDDEN_HEADERS = { | ||
"accept-charset": true, | ||
"accept-encoding": true, | ||
"access-control-request-headers": true, | ||
"access-control-request-method": true, | ||
connection: true, | ||
"content-length": true, | ||
cookie: true, | ||
date: true, | ||
dnt: true, | ||
expect: true, | ||
host: true, | ||
"keep-alive": true, | ||
origin: true, | ||
"permissions-policy": true, | ||
// No currently available Typescript technique allows you to match on a prefix. | ||
// 'proxy-':true, | ||
// 'sec-':true, | ||
referer: true, | ||
te: true, | ||
trailer: true, | ||
"transfer-encoding": true, | ||
upgrade: true, | ||
via: true | ||
}; | ||
function assertIsAllowedHttpRequestHeaders(headers) { | ||
const badHeaders = Object.keys(headers).filter((headerName) => { | ||
const lowercaseHeaderName = headerName.toLowerCase(); | ||
return DISALLOWED_HEADERS[headerName.toLowerCase()] === true || FORBIDDEN_HEADERS[headerName.toLowerCase()] === true || lowercaseHeaderName.startsWith("proxy-") || lowercaseHeaderName.startsWith("sec-"); | ||
}); | ||
if (badHeaders.length > 0) { | ||
throw new Error( | ||
`${badHeaders.length > 1 ? "These headers are" : "This header is"} forbidden: \`${badHeaders.join("`, `")}\`. Learn more at https://developer.mozilla.org/en-US/docs/Glossary/Forbidden_header_name.` | ||
); | ||
} | ||
} | ||
function normalizeHeaders(headers) { | ||
const out = {}; | ||
for (const headerName in headers) { | ||
out[headerName.toLowerCase()] = headers[headerName]; | ||
} | ||
return out; | ||
} | ||
var f = t; | ||
// src/http-request.ts | ||
async function makeHttpRequest({ headers, payload, signal, url }) { | ||
const body = JSON.stringify(payload); | ||
const requestInfo = { | ||
body: JSON.stringify(payload), | ||
body, | ||
headers: { | ||
"Content-type": "application/json" | ||
...headers && normalizeHeaders(headers), | ||
// Keep these headers lowercase so they will override any user-supplied headers above. | ||
accept: "application/json", | ||
"content-length": body.length.toString(), | ||
"content-type": "application/json; charset=utf-8" | ||
}, | ||
method: "POST" | ||
method: "POST", | ||
signal | ||
}; | ||
let response; | ||
{ | ||
response = await fetchImplNode(url, requestInfo); | ||
} | ||
const response = await f(url, requestInfo); | ||
if (!response.ok) { | ||
@@ -36,3 +95,3 @@ throw new SolanaHttpError({ | ||
// src/json-rpc-errors.ts | ||
// src/json-rpc-transport/json-rpc-errors.ts | ||
var SolanaJsonRpcError = class extends Error { | ||
@@ -50,3 +109,3 @@ constructor(details) { | ||
// src/json-rpc-message-id.ts | ||
// src/json-rpc-transport/json-rpc-message-id.ts | ||
var _nextMessageId = 0; | ||
@@ -59,3 +118,3 @@ function getNextMessageId() { | ||
// src/json-rpc-message.ts | ||
// src/json-rpc-transport/json-rpc-message.ts | ||
function createJsonRpcMessage(method, params) { | ||
@@ -70,51 +129,48 @@ return { | ||
// src/params-patcher.ts | ||
function visitNode(value, keyPath, onIntegerOverflow) { | ||
if (Array.isArray(value)) { | ||
return value.map( | ||
(element, ii) => visitNode(element, [...keyPath, ii], onIntegerOverflow) | ||
); | ||
} else if (typeof value === "object" && value !== null) { | ||
const out = {}; | ||
for (const propName in value) { | ||
if (Object.prototype.hasOwnProperty.call(value, propName)) { | ||
out[propName] = visitNode(value[propName], [...keyPath, propName], onIntegerOverflow); | ||
// src/json-rpc-transport/index.ts | ||
function createArmedJsonRpcTransport(transportConfig, pendingRequest) { | ||
const overrides = { | ||
async send(options) { | ||
const { methodName, params, responseProcessor } = pendingRequest; | ||
const payload = createJsonRpcMessage(methodName, params); | ||
const response = await makeHttpRequest({ | ||
headers: transportConfig.headers, | ||
payload, | ||
signal: options?.abortSignal, | ||
url: transportConfig.url | ||
}); | ||
if ("error" in response) { | ||
throw new SolanaJsonRpcError(response.error); | ||
} else { | ||
return responseProcessor ? responseProcessor(response.result) : response.result; | ||
} | ||
} | ||
return out; | ||
} else if (typeof value === "bigint") { | ||
if (onIntegerOverflow && (value > Number.MAX_SAFE_INTEGER || value < -Number.MAX_SAFE_INTEGER)) { | ||
onIntegerOverflow(keyPath, value); | ||
} | ||
return Number(value); | ||
} else { | ||
return value; | ||
} | ||
} | ||
function patchParamsForSolanaLabsRpc(params, onIntegerOverflow) { | ||
return visitNode(params, [], onIntegerOverflow); | ||
} | ||
// src/json-rpc-transport.ts | ||
function createArmedJsonRpcTransport(transportConfig, pendingMessage) { | ||
const { url } = transportConfig; | ||
const transport = { | ||
async send() { | ||
return await sendPayload(pendingMessage, url); | ||
} | ||
}; | ||
return makeProxy(transport, transportConfig, pendingMessage); | ||
return makeProxy(transportConfig, overrides, pendingRequest); | ||
} | ||
function createArmedBatchJsonRpcTransport(transportConfig, pendingMessages) { | ||
const { url } = transportConfig; | ||
const transport = { | ||
async sendBatch() { | ||
return await sendPayload(pendingMessages, url); | ||
function createArmedBatchJsonRpcTransport(transportConfig, pendingRequests) { | ||
const overrides = { | ||
async sendBatch(options) { | ||
const payload = pendingRequests.map(({ methodName, params }) => createJsonRpcMessage(methodName, params)); | ||
const responses = await makeHttpRequest({ | ||
headers: transportConfig.headers, | ||
payload, | ||
signal: options?.abortSignal, | ||
url: transportConfig.url | ||
}); | ||
const requestOrder = payload.map((p) => p.id); | ||
return responses.sort((a, b) => requestOrder.indexOf(a.id) - requestOrder.indexOf(b.id)).map((response, ii) => { | ||
if ("error" in response) { | ||
throw new SolanaJsonRpcError(response.error); | ||
} else { | ||
const { responseProcessor } = pendingRequests[ii]; | ||
return responseProcessor ? responseProcessor(response.result) : response.result; | ||
} | ||
}); | ||
} | ||
}; | ||
return makeProxy(transport, transportConfig, pendingMessages); | ||
return makeProxy(transportConfig, overrides, pendingRequests); | ||
} | ||
function makeProxy(transport, transportConfig, pendingRequestOrRequests) { | ||
const { onIntegerOverflow } = transportConfig; | ||
return new Proxy(transport, { | ||
function makeProxy(transportConfig, overrides, pendingRequestOrRequests) { | ||
return new Proxy(transportConfig.api, { | ||
defineProperty() { | ||
@@ -126,18 +182,15 @@ return false; | ||
}, | ||
get(...args) { | ||
const [target, p] = args; | ||
return p in target ? Reflect.get(...args) : function(...params) { | ||
get(target, p, receiver) { | ||
if (overrides && Reflect.has(overrides, p)) { | ||
return Reflect.get(overrides, p, receiver); | ||
} | ||
return function(...rawParams) { | ||
const methodName = p.toString(); | ||
const patchedParams = patchParamsForSolanaLabsRpc( | ||
params, | ||
onIntegerOverflow ? (keyPath, value) => { | ||
onIntegerOverflow(methodName, keyPath, value); | ||
} : void 0 | ||
); | ||
const newMessage = createJsonRpcMessage(methodName, patchedParams); | ||
const createTransportRequest = Reflect.get(target, methodName, receiver); | ||
const newRequest = createTransportRequest ? createTransportRequest(...rawParams) : { methodName, params: rawParams }; | ||
if (pendingRequestOrRequests == null) { | ||
return createArmedJsonRpcTransport(transportConfig, newMessage); | ||
return createArmedJsonRpcTransport(transportConfig, newRequest); | ||
} else { | ||
const nextPendingMessages = Array.isArray(pendingRequestOrRequests) ? [...pendingRequestOrRequests, newMessage] : [pendingRequestOrRequests, newMessage]; | ||
return createArmedBatchJsonRpcTransport(transportConfig, nextPendingMessages); | ||
const nextPendingRequests = Array.isArray(pendingRequestOrRequests) ? [...pendingRequestOrRequests, newRequest] : [pendingRequestOrRequests, newRequest]; | ||
return createArmedBatchJsonRpcTransport(transportConfig, nextPendingRequests); | ||
} | ||
@@ -148,25 +201,8 @@ }; | ||
} | ||
function processResponse(response) { | ||
if ("error" in response) { | ||
throw new SolanaJsonRpcError(response.error); | ||
} else { | ||
return response.result; | ||
function createJsonRpcTransport(transportConfig) { | ||
if (__DEV__ && transportConfig.headers) { | ||
assertIsAllowedHttpRequestHeaders(transportConfig.headers); | ||
} | ||
return makeProxy(transportConfig); | ||
} | ||
async function sendPayload(payload, url) { | ||
const responseOrResponses = await makeHttpRequest({ | ||
payload, | ||
url | ||
}); | ||
if (Array.isArray(responseOrResponses)) { | ||
const requestOrder = payload.map((p) => p.id); | ||
return responseOrResponses.sort((a, b) => requestOrder.indexOf(a.id) - requestOrder.indexOf(b.id)).map(processResponse); | ||
} else { | ||
return processResponse(responseOrResponses); | ||
} | ||
} | ||
function createJsonRpcTransport(transportConfig) { | ||
const transport = {}; | ||
return makeProxy(transport, transportConfig); | ||
} | ||
@@ -173,0 +209,0 @@ export { createJsonRpcTransport }; |
@@ -0,7 +1,10 @@ | ||
import { AllowedHttpRequestHeaders } from './http-request-headers'; | ||
type Config = Readonly<{ | ||
headers?: AllowedHttpRequestHeaders; | ||
payload: unknown; | ||
signal?: AbortSignal; | ||
url: string; | ||
}>; | ||
export declare function makeHttpRequest<TResponse>({ payload, url }: Config): Promise<TResponse>; | ||
export declare function makeHttpRequest<TResponse>({ headers, payload, signal, url }: Config): Promise<TResponse>; | ||
export {}; | ||
//# sourceMappingURL=http-request.d.ts.map |
{ | ||
"name": "@solana/rpc-transport", | ||
"version": "0.0.0-experimental.2", | ||
"version": "2.0.0-experimental.3290268", | ||
"description": "Network transports for accessing the Solana JSON RPC API", | ||
@@ -49,11 +49,10 @@ "exports": { | ||
"devDependencies": { | ||
"@solana/eslint-config-solana": "^0.0.4", | ||
"@solana/eslint-config-solana": "^1.0.0", | ||
"@swc/core": "^1.3.18", | ||
"@swc/jest": "^0.2.23", | ||
"@types/jest": "^29.5.0", | ||
"@types/node-fetch": "2", | ||
"@typescript-eslint/eslint-plugin": "^5.43.0", | ||
"@typescript-eslint/parser": "^5.43.0", | ||
"agadoo": "^2.0.0", | ||
"eslint": "^8.27.0", | ||
"@typescript-eslint/eslint-plugin": "^5.57.1", | ||
"@typescript-eslint/parser": "^5.57.1", | ||
"agadoo": "^3.0.0", | ||
"eslint": "^8.37.0", | ||
"eslint-plugin-jest": "^27.1.5", | ||
@@ -64,4 +63,4 @@ "eslint-plugin-react-hooks": "^4.6.0", | ||
"jest-environment-jsdom": "^29.5.0", | ||
"jest-fetch-mock": "^3.0.3", | ||
"jest-runner-eslint": "^1.1.0", | ||
"jest-fetch-mock-fork": "^3.0.4", | ||
"jest-runner-eslint": "^2.0.0", | ||
"jest-runner-prettier": "^1.0.0", | ||
@@ -71,7 +70,7 @@ "postcss": "^8.4.12", | ||
"ts-node": "^10.9.1", | ||
"tsup": "6.5.0", | ||
"turbo": "^1.6.3", | ||
"typescript": "^4.9", | ||
"@solana/fetch-impl-browser": "0.0.0-development", | ||
"tsup": "6.7.0", | ||
"typescript": "^5.0.3", | ||
"version-from-git": "^1.1.1", | ||
"build-scripts": "0.0.0", | ||
"fetch-impl": "0.0.0", | ||
"test-config": "0.0.0", | ||
@@ -89,5 +88,3 @@ "tsconfig": "0.0.0" | ||
"dependencies": { | ||
"@solana/keys": "^0.0.0-experimental.1", | ||
"@solana/rpc-core": "^0.0.0-experimental.2", | ||
"node-fetch": "^2.6.7" | ||
"node-fetch": "^3.3.1" | ||
}, | ||
@@ -98,2 +95,3 @@ "scripts": { | ||
"dev": "jest -c node_modules/test-config/jest-dev.config.ts --rootDir . --watch", | ||
"publish-packages": "pnpm publish --tag experimental --access public --no-git-checks", | ||
"test:lint": "jest -c node_modules/test-config/jest-lint.config.ts --rootDir . --silent", | ||
@@ -100,0 +98,0 @@ "test:prettier": "jest -c node_modules/test-config/jest-prettier.config.ts --rootDir . --silent", |
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
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
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
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
147504
1
26
1322
0
+ Addeddata-uri-to-buffer@4.0.1(transitive)
+ Addedfetch-blob@3.2.0(transitive)
+ Addedformdata-polyfill@4.0.10(transitive)
+ Addednode-domexception@1.0.0(transitive)
+ Addednode-fetch@3.3.2(transitive)
+ Addedweb-streams-polyfill@3.3.3(transitive)
- Removed@solana/keys@^0.0.0-experimental.1
- Removed@metaplex-foundation/umi-options@0.8.9(transitive)
- Removed@metaplex-foundation/umi-public-keys@0.8.9(transitive)
- Removed@metaplex-foundation/umi-serializers@0.8.9(transitive)
- Removed@metaplex-foundation/umi-serializers-core@0.8.9(transitive)
- Removed@metaplex-foundation/umi-serializers-encodings@0.8.9(transitive)
- Removed@metaplex-foundation/umi-serializers-numbers@0.8.9(transitive)
- Removed@solana/keys@0.0.0-experimental.1(transitive)
- Removed@solana/rpc-core@0.0.0-experimental.df45965(transitive)
- Removedbase-x@4.0.0(transitive)
- Removedbs58@5.0.0(transitive)
- Removednode-fetch@2.7.0(transitive)
- Removedtr46@0.0.3(transitive)
- Removedwebidl-conversions@3.0.1(transitive)
- Removedwhatwg-url@5.0.0(transitive)
Updatednode-fetch@^3.3.1