fix(virtualstream): reconstitute JSON-serialized binary data in VirtualStream; update docs, build config, and dependency bumps
This commit is contained in:
128
.gitlab-ci.yml
128
.gitlab-ci.yml
@@ -1,128 +0,0 @@
|
||||
# gitzone ci_default
|
||||
image: registry.gitlab.com/hosttoday/ht-docker-node:npmci
|
||||
|
||||
cache:
|
||||
paths:
|
||||
- .npmci_cache/
|
||||
key: '$CI_BUILD_STAGE'
|
||||
|
||||
stages:
|
||||
- security
|
||||
- test
|
||||
- release
|
||||
- metadata
|
||||
|
||||
before_script:
|
||||
- npm install -g @shipzone/npmci
|
||||
|
||||
# ====================
|
||||
# security stage
|
||||
# ====================
|
||||
auditProductionDependencies:
|
||||
image: registry.gitlab.com/hosttoday/ht-docker-node:npmci
|
||||
stage: security
|
||||
script:
|
||||
- npmci npm prepare
|
||||
- npmci command npm install --production --ignore-scripts
|
||||
- npmci command npm config set registry https://registry.npmjs.org
|
||||
- npmci command npm audit --audit-level=high --only=prod --production
|
||||
tags:
|
||||
- docker
|
||||
allow_failure: true
|
||||
|
||||
auditDevDependencies:
|
||||
image: registry.gitlab.com/hosttoday/ht-docker-node:npmci
|
||||
stage: security
|
||||
script:
|
||||
- npmci npm prepare
|
||||
- npmci command npm install --ignore-scripts
|
||||
- npmci command npm config set registry https://registry.npmjs.org
|
||||
- npmci command npm audit --audit-level=high --only=dev
|
||||
tags:
|
||||
- docker
|
||||
allow_failure: true
|
||||
|
||||
# ====================
|
||||
# test stage
|
||||
# ====================
|
||||
|
||||
testStable:
|
||||
stage: test
|
||||
script:
|
||||
- npmci npm prepare
|
||||
- npmci node install stable
|
||||
- npmci npm install
|
||||
- npmci npm test
|
||||
coverage: /\d+.?\d+?\%\s*coverage/
|
||||
tags:
|
||||
- docker
|
||||
|
||||
testBuild:
|
||||
stage: test
|
||||
script:
|
||||
- npmci npm prepare
|
||||
- npmci node install stable
|
||||
- npmci npm install
|
||||
- npmci command npm run build
|
||||
coverage: /\d+.?\d+?\%\s*coverage/
|
||||
tags:
|
||||
- docker
|
||||
|
||||
release:
|
||||
stage: release
|
||||
script:
|
||||
- npmci node install stable
|
||||
- npmci npm publish
|
||||
only:
|
||||
- tags
|
||||
tags:
|
||||
- lossless
|
||||
- docker
|
||||
- notpriv
|
||||
|
||||
# ====================
|
||||
# metadata stage
|
||||
# ====================
|
||||
codequality:
|
||||
stage: metadata
|
||||
allow_failure: true
|
||||
only:
|
||||
- tags
|
||||
script:
|
||||
- npmci command npm install -g typescript
|
||||
- npmci npm prepare
|
||||
- npmci npm install
|
||||
tags:
|
||||
- lossless
|
||||
- docker
|
||||
- priv
|
||||
|
||||
trigger:
|
||||
stage: metadata
|
||||
script:
|
||||
- npmci trigger
|
||||
only:
|
||||
- tags
|
||||
tags:
|
||||
- lossless
|
||||
- docker
|
||||
- notpriv
|
||||
|
||||
pages:
|
||||
stage: metadata
|
||||
script:
|
||||
- npmci node install stable
|
||||
- npmci npm prepare
|
||||
- npmci npm install
|
||||
- npmci command npm run buildDocs
|
||||
tags:
|
||||
- lossless
|
||||
- docker
|
||||
- notpriv
|
||||
only:
|
||||
- tags
|
||||
artifacts:
|
||||
expire_in: 1 week
|
||||
paths:
|
||||
- public
|
||||
allow_failure: true
|
||||
@@ -1,5 +1,14 @@
|
||||
# Changelog
|
||||
|
||||
## 2026-03-01 - 3.2.7 - fix(virtualstream)
|
||||
reconstitute JSON-serialized binary data in VirtualStream; update docs, build config, and dependency bumps
|
||||
|
||||
- Add VirtualStream.reconstituteBinaryData to restore Buffer/Uint8Array shapes lost to JSON serialization and use it when handling chunk data and response chunkData.
|
||||
- Remove .gitlab-ci.yml and add a tsbundle bundle config in npmextra.json; update package.json build script to invoke tsbundle without the previous extra argument.
|
||||
- Bump multiple devDependencies and dependencies to newer patch/minor versions.
|
||||
- Significantly expand README with usage examples for TypedRequest, TypedHandler, TypedRouter, TypedTarget, VirtualStream, error handling, hooks, and architecture overview.
|
||||
- Minor README/legal wording clarifications.
|
||||
|
||||
## 2026-02-11 - 3.2.6 - fix(deps)
|
||||
upgrade @push.rocks/webrequest to ^4.0.1 and adapt TypedRequest to new API
|
||||
|
||||
|
||||
@@ -36,5 +36,16 @@
|
||||
},
|
||||
"@ship.zone/szci": {
|
||||
"npmGlobalTools": []
|
||||
},
|
||||
"@git.zone/tsbundle": {
|
||||
"bundles": [
|
||||
{
|
||||
"from": "./ts/index.ts",
|
||||
"to": "./dist_bundle/bundle.js",
|
||||
"outputMode": "bundle",
|
||||
"bundler": "esbuild",
|
||||
"production": true
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
26
package.json
26
package.json
@@ -10,28 +10,28 @@
|
||||
"license": "MIT",
|
||||
"scripts": {
|
||||
"test": "(tstest test/)",
|
||||
"build": "(tsbuild --web --allowimplicitany && tsbundle npm)",
|
||||
"build": "(tsbuild --web --allowimplicitany && tsbundle)",
|
||||
"buildDocs": "tsdoc"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@api.global/typedserver": "^3.0.51",
|
||||
"@git.zone/tsbuild": "^2.1.84",
|
||||
"@git.zone/tsbundle": "^2.0.15",
|
||||
"@git.zone/tsrun": "^1.2.49",
|
||||
"@git.zone/tstest": "^1.0.90",
|
||||
"@push.rocks/smartenv": "^5.0.12",
|
||||
"@push.rocks/tapbundle": "^5.3.0",
|
||||
"@types/node": "^22.7.5"
|
||||
"@api.global/typedserver": "^3.0.80",
|
||||
"@git.zone/tsbuild": "^2.7.3",
|
||||
"@git.zone/tsbundle": "^2.9.0",
|
||||
"@git.zone/tsrun": "^1.6.2",
|
||||
"@git.zone/tstest": "^1.11.5",
|
||||
"@push.rocks/smartenv": "^5.0.13",
|
||||
"@push.rocks/tapbundle": "^5.6.3",
|
||||
"@types/node": "^22.19.13"
|
||||
},
|
||||
"dependencies": {
|
||||
"@api.global/typedrequest-interfaces": "^3.0.19",
|
||||
"@push.rocks/isounique": "^1.0.5",
|
||||
"@push.rocks/lik": "^6.1.0",
|
||||
"@push.rocks/smartbuffer": "^3.0.4",
|
||||
"@push.rocks/lik": "^6.3.1",
|
||||
"@push.rocks/smartbuffer": "^3.0.5",
|
||||
"@push.rocks/smartdelay": "^3.0.5",
|
||||
"@push.rocks/smartguard": "^3.1.0",
|
||||
"@push.rocks/smartpromise": "^4.0.4",
|
||||
"@push.rocks/webrequest": "^4.0.1",
|
||||
"@push.rocks/smartpromise": "^4.2.3",
|
||||
"@push.rocks/webrequest": "^4.0.2",
|
||||
"@push.rocks/webstream": "^1.0.10"
|
||||
},
|
||||
"files": [
|
||||
|
||||
8195
pnpm-lock.yaml
generated
8195
pnpm-lock.yaml
generated
File diff suppressed because it is too large
Load Diff
340
readme.md
340
readme.md
@@ -1,135 +1,305 @@
|
||||
# @api.global/typedrequest
|
||||
|
||||
A TypeScript library for making typed requests towards APIs, providing interfaces and classes to handle request/response cycles using typed definitions. It supports building and handling requests, routing based on request types, and virtual stream handling over networks.
|
||||
A TypeScript library for making **fully typed request/response cycles** across any transport — HTTP, WebSockets, broadcast channels, or custom protocols. Define your API contract once as a TypeScript interface, then use it on both client and server with compile-time safety, automatic routing, virtual streams for real-time data, and built-in traffic monitoring hooks.
|
||||
|
||||
## Issue Reporting and Security
|
||||
|
||||
For reporting bugs, issues, or security vulnerabilities, please visit [community.foss.global/](https://community.foss.global/). This is the central community hub for all issue reporting. Developers who sign and comply with our contribution agreement and go through identification can also get a [code.foss.global/](https://code.foss.global/) account to submit Pull Requests directly.
|
||||
|
||||
## Install
|
||||
|
||||
To install `@api.global/typedrequest`, you can use npm or yarn. Run the following command in your project directory:
|
||||
|
||||
```bash
|
||||
pnpm install @api.global/typedrequest
|
||||
```
|
||||
npm install @api.global/typedrequest --save
|
||||
```
|
||||
|
||||
Or, if you prefer yarn:
|
||||
|
||||
```
|
||||
yarn add @api.global/typedrequest
|
||||
```
|
||||
|
||||
This package is designed to facilitate making typed requests to APIs, ensuring that both requests and responses adhere to predefined interfaces. This enhances the predictability and reliability of network communication in applications that interact with APIs.
|
||||
|
||||
## Usage
|
||||
|
||||
Utilizing `@api.global/typedrequest` involves several core concepts, including the creation of typed requests, handling virtual streams, defining request handlers, and routing requests based on their type. Below are comprehensive examples and explanations of how to use these features in your applications.
|
||||
|
||||
### Setting Up a Typed Request
|
||||
|
||||
The foundation of `@api.global/typedrequest` is the `TypedRequest` class, which allows you to define requests that are strictly typed. This ensures that both the request sent and the response received match the expected structures.
|
||||
|
||||
First, define an interface that represents your request and response data structure:
|
||||
All examples use ESM imports and TypeScript.
|
||||
|
||||
```typescript
|
||||
// Define an interface for your request/response structure
|
||||
interface IUserRequest {
|
||||
import {
|
||||
TypedRequest,
|
||||
TypedHandler,
|
||||
TypedRouter,
|
||||
TypedTarget,
|
||||
VirtualStream,
|
||||
TypedResponseError,
|
||||
} from '@api.global/typedrequest';
|
||||
```
|
||||
|
||||
### 🔑 Define Your API Contract
|
||||
|
||||
Every request/response pair is described by a simple interface extending `ITypedRequest`:
|
||||
|
||||
```typescript
|
||||
interface IGetUser {
|
||||
method: 'getUser';
|
||||
request: { userId: string };
|
||||
response: { username: string; email: string; };
|
||||
response: { username: string; email: string };
|
||||
}
|
||||
```
|
||||
|
||||
Next, create an instance of `TypedRequest` using this interface, and use it to make an API call:
|
||||
|
||||
```typescript
|
||||
import { TypedRequest } from '@api.global/typedrequest';
|
||||
|
||||
// Construct a TypedRequest instance for fetching user data
|
||||
const getUserRequest = new TypedRequest<IUserRequest>('https://your-api.com/users', 'getUser');
|
||||
|
||||
// Execute the request with a specific userId and log the response
|
||||
const userResponse = await getUserRequest.fire({ userId: 'user-123' });
|
||||
console.log(userResponse.username);
|
||||
```
|
||||
|
||||
### Handling Virtual Streams
|
||||
|
||||
`@api.global/typedrequest` introduces the concept of virtual streams, allowing you to manage real-time data transfer over the network as streams. You can create a virtual stream, send data, and read data from it as shown below:
|
||||
|
||||
```typescript
|
||||
import { VirtualStream } from '@api.global/typedrequest';
|
||||
|
||||
// Instantiate a VirtualStream for handling real-time data
|
||||
const myStream = new VirtualStream<ArrayBufferLike>();
|
||||
|
||||
// Use the stream to send data
|
||||
await myStream.sendData(new TextEncoder().encode("Hello, World!"));
|
||||
|
||||
// Retrieve data from the stream
|
||||
const receivedData = await myStream.fetchData();
|
||||
console.log(new TextDecoder().decode(receivedData));
|
||||
```
|
||||
|
||||
### Defining and Using Handlers
|
||||
|
||||
To process requests, you can define handlers that are responsible for specific types of requests:
|
||||
|
||||
```typescript
|
||||
import { TypedHandler } from '@api.global/typedrequest';
|
||||
|
||||
interface IAdditionRequest {
|
||||
interface IAddNumbers {
|
||||
method: 'add';
|
||||
request: { a: number; b: number; };
|
||||
response: { result: number; };
|
||||
request: { a: number; b: number };
|
||||
response: { result: number };
|
||||
}
|
||||
```
|
||||
|
||||
// Create a handler for processing addition requests
|
||||
const additionHandler = new TypedHandler<IAdditionRequest>('add', async (req) => {
|
||||
The `method` field acts as a discriminator — it's what the router uses to match requests to handlers.
|
||||
|
||||
### 📡 Making Typed Requests (Client Side)
|
||||
|
||||
`TypedRequest` fires a typed request against an HTTP endpoint or a `TypedTarget`:
|
||||
|
||||
```typescript
|
||||
// Against an HTTP endpoint
|
||||
const getUser = new TypedRequest<IGetUser>('https://api.example.com/rpc', 'getUser');
|
||||
const user = await getUser.fire({ userId: 'user-123' });
|
||||
console.log(user.username); // fully typed!
|
||||
|
||||
// With response caching
|
||||
const cachedUser = await getUser.fire({ userId: 'user-123' }, true);
|
||||
```
|
||||
|
||||
### 🛠️ Handling Requests (Server Side)
|
||||
|
||||
`TypedHandler` processes a specific method and returns a typed response:
|
||||
|
||||
```typescript
|
||||
const addHandler = new TypedHandler<IAddNumbers>('add', async (req) => {
|
||||
return { result: req.a + req.b };
|
||||
});
|
||||
```
|
||||
|
||||
### Routing Requests
|
||||
|
||||
`TypedRouter` is used to route requests to their appropriate handlers based on the request type:
|
||||
The second argument to the handler function is an optional `TypedTools` instance that gives access to guard validation and transport-layer context:
|
||||
|
||||
```typescript
|
||||
import { TypedRouter } from '@api.global/typedrequest';
|
||||
const secureHandler = new TypedHandler<IGetUser>('getUser', async (req, tools) => {
|
||||
// Access transport-layer context (e.g., authenticated user info)
|
||||
const peer = tools.localData.peer;
|
||||
|
||||
// Validate with guards
|
||||
await tools.passGuards([myAuthGuard], req);
|
||||
|
||||
return { username: 'Alice', email: 'alice@example.com' };
|
||||
});
|
||||
```
|
||||
|
||||
### 🚦 Routing Requests
|
||||
|
||||
`TypedRouter` dispatches incoming requests to the correct handler based on the `method` field:
|
||||
|
||||
```typescript
|
||||
const router = new TypedRouter();
|
||||
router.addTypedHandler(addHandler);
|
||||
router.addTypedHandler(secureHandler);
|
||||
|
||||
// Route an incoming request object
|
||||
const response = await router.routeAndAddResponse(incomingTypedRequest);
|
||||
```
|
||||
|
||||
Routers are **composable** — you can nest them to build modular API architectures:
|
||||
|
||||
```typescript
|
||||
const coreRouter = new TypedRouter();
|
||||
const authRouter = new TypedRouter();
|
||||
|
||||
// Each sub-router manages its own handlers
|
||||
coreRouter.addTypedHandler(addHandler);
|
||||
authRouter.addTypedHandler(secureHandler);
|
||||
|
||||
// Link them together — requests flow through the entire chain
|
||||
const mainRouter = new TypedRouter();
|
||||
mainRouter.addTypedRouter(coreRouter);
|
||||
mainRouter.addTypedRouter(authRouter);
|
||||
```
|
||||
|
||||
### 🎯 Custom Targets with TypedTarget
|
||||
|
||||
For non-HTTP transports (WebSockets, broadcast channels, IPC), use `TypedTarget` with a custom post function:
|
||||
|
||||
```typescript
|
||||
// Synchronous target (post returns the response directly)
|
||||
const target = new TypedTarget({
|
||||
postMethod: async (payload) => {
|
||||
// Send via your custom transport and return the response
|
||||
return await myWebSocket.sendAndWait(payload);
|
||||
},
|
||||
});
|
||||
|
||||
const request = new TypedRequest<IGetUser>(target, 'getUser');
|
||||
const user = await request.fire({ userId: 'user-123' });
|
||||
```
|
||||
|
||||
For **async targets** where the response arrives separately (e.g., WebSocket push), pair a `TypedTarget` with a `TypedRouter`:
|
||||
|
||||
```typescript
|
||||
const router = new TypedRouter();
|
||||
|
||||
// Associate the additionHandler with the router
|
||||
router.addTypedHandler(additionHandler);
|
||||
const asyncTarget = new TypedTarget({
|
||||
postMethodWithTypedRouter: async (payload) => {
|
||||
// Fire-and-forget — response will arrive via router
|
||||
mySocket.send(JSON.stringify(payload));
|
||||
},
|
||||
typedRouterRef: router,
|
||||
});
|
||||
|
||||
// The router now directs 'add' requests to the additionHandler
|
||||
// When the response arrives later, route it back:
|
||||
mySocket.onMessage((data) => {
|
||||
router.routeAndAddResponse(JSON.parse(data));
|
||||
});
|
||||
```
|
||||
|
||||
### Error Handling
|
||||
### 🌊 Virtual Streams
|
||||
|
||||
`@api.global/typedrequest` provides a `TypedResponseError` class to facilitate error handling in typed requests:
|
||||
`VirtualStream` enables **bidirectional binary streaming** over any transport that supports typed requests. Data is automatically chunked with backpressure control:
|
||||
|
||||
```typescript
|
||||
import { TypedResponseError } from '@api.global/typedrequest';
|
||||
import type { IVirtualStream } from '@api.global/typedrequest-interfaces';
|
||||
|
||||
// Throw a TypedResponseError with details about the error
|
||||
throw new TypedResponseError('An error occurred', { detail: 'Error details' });
|
||||
// Define a streaming endpoint
|
||||
interface IFileUpload {
|
||||
method: 'uploadFile';
|
||||
request: { filename: string; dataStream: IVirtualStream };
|
||||
response: { bytesReceived: number };
|
||||
}
|
||||
|
||||
// Client side: create and send a stream
|
||||
const uploadStream = new VirtualStream<Uint8Array>();
|
||||
const upload = new TypedRequest<IFileUpload>('https://api.example.com/rpc', 'uploadFile');
|
||||
|
||||
const responsePromise = upload.fire({
|
||||
filename: 'data.bin',
|
||||
dataStream: uploadStream,
|
||||
});
|
||||
|
||||
// Send data through the stream
|
||||
await uploadStream.sendData(new TextEncoder().encode('Hello, World!'));
|
||||
await uploadStream.close();
|
||||
|
||||
const result = await responsePromise;
|
||||
console.log(result.bytesReceived);
|
||||
```
|
||||
|
||||
This comprehensive overview covers the essential features provided by `@api.global/typedrequest`. By adhering to defined request and response structures, you can create more reliable and maintainable applications that interact seamlessly with APIs.
|
||||
```typescript
|
||||
// Server side: receive and process the stream
|
||||
const uploadHandler = new TypedHandler<IFileUpload>('uploadFile', async (req) => {
|
||||
let total = 0;
|
||||
// Read chunks from the stream
|
||||
const chunk = await req.dataStream.fetchData();
|
||||
total += chunk.byteLength;
|
||||
|
||||
return { bytesReceived: total };
|
||||
});
|
||||
```
|
||||
|
||||
VirtualStreams also integrate with the Web Streams API:
|
||||
|
||||
```typescript
|
||||
// Pipe a ReadableStream into a VirtualStream
|
||||
await virtualStream.readFromWebstream(readableStream);
|
||||
|
||||
// Pipe a VirtualStream into a WritableStream
|
||||
await virtualStream.writeToWebstream(writableStream);
|
||||
```
|
||||
|
||||
### ⚠️ Error Handling
|
||||
|
||||
Throw `TypedResponseError` inside handlers to send structured errors back to the caller:
|
||||
|
||||
```typescript
|
||||
const handler = new TypedHandler<IGetUser>('getUser', async (req) => {
|
||||
const user = await db.findUser(req.userId);
|
||||
if (!user) {
|
||||
throw new TypedResponseError('User not found', { userId: req.userId });
|
||||
}
|
||||
return { username: user.name, email: user.email };
|
||||
});
|
||||
```
|
||||
|
||||
On the client side, `TypedResponseError` is thrown when the server responds with an error:
|
||||
|
||||
```typescript
|
||||
try {
|
||||
await getUser.fire({ userId: 'nonexistent' });
|
||||
} catch (err) {
|
||||
if (err instanceof TypedResponseError) {
|
||||
console.error(err.errorText); // 'User not found'
|
||||
console.error(err.errorData); // { userId: 'nonexistent' }
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### 📊 Traffic Monitoring Hooks
|
||||
|
||||
Monitor all TypedRequest traffic with global or per-router hooks:
|
||||
|
||||
```typescript
|
||||
// Global hooks — apply to ALL routers and requests across bundles
|
||||
TypedRouter.setGlobalHooks({
|
||||
onOutgoingRequest: (entry) => {
|
||||
console.log(`→ ${entry.method} [${entry.correlationId}]`);
|
||||
},
|
||||
onIncomingResponse: (entry) => {
|
||||
console.log(`← ${entry.method} took ${entry.durationMs}ms`);
|
||||
},
|
||||
onIncomingRequest: (entry) => {
|
||||
console.log(`⇒ handling ${entry.method}`);
|
||||
},
|
||||
onOutgoingResponse: (entry) => {
|
||||
if (entry.error) console.error(`⇐ ${entry.method} error: ${entry.error}`);
|
||||
},
|
||||
});
|
||||
|
||||
// Per-router hooks
|
||||
router.setHooks({
|
||||
onIncomingRequest: (entry) => metrics.trackRequest(entry),
|
||||
});
|
||||
|
||||
// Skip hooks for internal requests (e.g., health checks)
|
||||
const internalReq = new TypedRequest<IHealthCheck>(target, 'healthCheck');
|
||||
internalReq.skipHooks = true;
|
||||
```
|
||||
|
||||
### 🏗️ Architecture Overview
|
||||
|
||||
```
|
||||
┌─────────────┐ ┌──────────────┐ ┌──────────────┐
|
||||
│ TypedRequest │──────▶│ HTTP / WS / │──────▶│ TypedRouter │
|
||||
│ (client) │ │ TypedTarget │ │ (server) │
|
||||
└─────────────┘ └──────────────┘ └──────┬───────┘
|
||||
│
|
||||
┌──────▼───────┐
|
||||
│ TypedHandler │
|
||||
│ (your logic) │
|
||||
└──────────────┘
|
||||
```
|
||||
|
||||
- **TypedRequest** — fires typed requests against a URL or TypedTarget
|
||||
- **TypedTarget** — abstracts the transport layer (HTTP, WebSocket, custom)
|
||||
- **TypedRouter** — routes incoming requests to the correct handler; composable via `addTypedRouter()`
|
||||
- **TypedHandler** — processes a single method and returns a typed response
|
||||
- **VirtualStream** — bidirectional binary streaming with backpressure over any supported transport
|
||||
- **TypedResponseError** — structured error propagation
|
||||
- **TypedTools** — guard validation and transport-layer context in handlers
|
||||
|
||||
## License and Legal Information
|
||||
|
||||
This repository contains open-source code that is licensed under the MIT License. A copy of the MIT License can be found in the [license](license) file within this repository.
|
||||
This repository contains open-source code licensed under the MIT License. A copy of the license can be found in the [LICENSE](./LICENSE) file.
|
||||
|
||||
**Please note:** The MIT License does not grant permission to use the trade names, trademarks, service marks, or product names of the project, except as required for reasonable and customary use in describing the origin of the work and reproducing the content of the NOTICE file.
|
||||
|
||||
### Trademarks
|
||||
|
||||
This project is owned and maintained by Task Venture Capital GmbH. The names and logos associated with Task Venture Capital GmbH and any related products or services are trademarks of Task Venture Capital GmbH and are not included within the scope of the MIT license granted herein. Use of these trademarks must comply with Task Venture Capital GmbH's Trademark Guidelines, and any usage must be approved in writing by Task Venture Capital GmbH.
|
||||
This project is owned and maintained by Task Venture Capital GmbH. The names and logos associated with Task Venture Capital GmbH and any related products or services are trademarks of Task Venture Capital GmbH or third parties, and are not included within the scope of the MIT license granted herein.
|
||||
|
||||
Use of these trademarks must comply with Task Venture Capital GmbH's Trademark Guidelines or the guidelines of the respective third-party owners, and any usage must be approved in writing. Third-party trademarks used herein are the property of their respective owners and used only in a descriptive manner, e.g. for an implementation of an API or similar.
|
||||
|
||||
### Company Information
|
||||
|
||||
Task Venture Capital GmbH
|
||||
Registered at District court Bremen HRB 35230 HB, Germany
|
||||
Task Venture Capital GmbH
|
||||
Registered at District Court Bremen HRB 35230 HB, Germany
|
||||
|
||||
For any legal inquiries or if you require further information, please contact us via email at hello@task.vc.
|
||||
For any legal inquiries or further information, please contact us via email at hello@task.vc.
|
||||
|
||||
By using this repository, you acknowledge that you have read this section, agree to comply with its terms, and understand that the licensing of the code does not imply endorsement by Task Venture Capital GmbH of any derivative works.
|
||||
|
||||
@@ -3,6 +3,6 @@
|
||||
*/
|
||||
export const commitinfo = {
|
||||
name: '@api.global/typedrequest',
|
||||
version: '3.2.6',
|
||||
version: '3.2.7',
|
||||
description: 'A TypeScript library for making typed requests towards APIs, including facilities for handling requests, routing, and virtual stream handling.'
|
||||
}
|
||||
|
||||
@@ -158,6 +158,35 @@ export class VirtualStream<T = Uint8Array> implements plugins.typedRequestInterf
|
||||
|
||||
constructor() {}
|
||||
|
||||
/**
|
||||
* Reconstitute binary data that was lost through JSON serialization.
|
||||
* Node.js Buffer becomes {type: "Buffer", data: [...]},
|
||||
* Uint8Array becomes {"0": 104, "1": 101, ...}.
|
||||
*/
|
||||
private static reconstituteBinaryData(data: any): any {
|
||||
if (data == null || data instanceof Uint8Array || (typeof Buffer !== 'undefined' && Buffer.isBuffer(data))) {
|
||||
return data;
|
||||
}
|
||||
if (typeof data === 'object') {
|
||||
// Handle JSON-serialized Node.js Buffer: {type: "Buffer", data: [...]}
|
||||
if (data.type === 'Buffer' && Array.isArray(data.data)) {
|
||||
return new Uint8Array(data.data);
|
||||
}
|
||||
// Handle JSON-serialized Uint8Array: {"0": 104, "1": 101, ...}
|
||||
const keys = Object.keys(data);
|
||||
if (keys.length > 0 && keys.every((k) => /^\d+$/.test(k))) {
|
||||
const arr = new Array(keys.length);
|
||||
for (const k of keys) {
|
||||
arr[Number(k)] = data[k];
|
||||
}
|
||||
if (arr.every((v) => typeof v === 'number')) {
|
||||
return new Uint8Array(arr);
|
||||
}
|
||||
}
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
workingDeferred: plugins.smartpromise.Deferred<void>;
|
||||
|
||||
/**
|
||||
@@ -231,7 +260,7 @@ export class VirtualStream<T = Uint8Array> implements plugins.typedRequestInterf
|
||||
});
|
||||
|
||||
if (streamTr && streamTr.response && streamTr.response.chunkData) {
|
||||
this.receiveBackpressuredArray.push(streamTr.response.chunkData);
|
||||
this.receiveBackpressuredArray.push(VirtualStream.reconstituteBinaryData(streamTr.response.chunkData) as T);
|
||||
}
|
||||
otherSideIsBackpressured = streamTr && streamTr.response && streamTr.response.backpressure;
|
||||
thisSideIsBackpressured = !this.receiveBackpressuredArray.checkSpaceAvailable();
|
||||
@@ -285,7 +314,7 @@ export class VirtualStream<T = Uint8Array> implements plugins.typedRequestInterf
|
||||
|
||||
// chunk handling
|
||||
if (streamTrArg.request.mainPurpose === 'chunk') {
|
||||
this.receiveBackpressuredArray.push(streamTrArg.request.chunkData);
|
||||
this.receiveBackpressuredArray.push(VirtualStream.reconstituteBinaryData(streamTrArg.request.chunkData) as T);
|
||||
if (this.sendBackpressuredArray.data.length > 0 && streamTrArg.response.backpressure === false) {
|
||||
const dataArg = this.sendBackpressuredArray.shift();
|
||||
streamTrArg.response = {
|
||||
|
||||
Reference in New Issue
Block a user