@nmtjs/http-client 0.15.0-beta.3 → 0.15.0-beta.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/package.json +8 -7
  2. package/src/index.ts +178 -0
package/package.json CHANGED
@@ -9,21 +9,22 @@
9
9
  }
10
10
  },
11
11
  "dependencies": {
12
- "@nmtjs/client": "0.15.0-beta.3",
13
- "@nmtjs/common": "0.15.0-beta.3",
14
- "@nmtjs/protocol": "0.15.0-beta.3"
12
+ "@nmtjs/protocol": "0.15.0-beta.4",
13
+ "@nmtjs/common": "0.15.0-beta.4",
14
+ "@nmtjs/client": "0.15.0-beta.4"
15
15
  },
16
16
  "peerDependencies": {
17
- "@nmtjs/client": "0.15.0-beta.3",
18
- "@nmtjs/protocol": "0.15.0-beta.3",
19
- "@nmtjs/common": "0.15.0-beta.3"
17
+ "@nmtjs/client": "0.15.0-beta.4",
18
+ "@nmtjs/protocol": "0.15.0-beta.4",
19
+ "@nmtjs/common": "0.15.0-beta.4"
20
20
  },
21
21
  "files": [
22
22
  "dist",
23
+ "src",
23
24
  "LICENSE.md",
24
25
  "README.md"
25
26
  ],
26
- "version": "0.15.0-beta.3",
27
+ "version": "0.15.0-beta.4",
27
28
  "scripts": {
28
29
  "clean-build": "rm -rf ./dist",
29
30
  "build": "tsc --declaration --sourcemap",
package/src/index.ts ADDED
@@ -0,0 +1,178 @@
1
+ import type {
2
+ ClientTransport,
3
+ ClientTransportFactory,
4
+ ClientTransportMessageOptions,
5
+ ClientTransportRpcParams,
6
+ } from '@nmtjs/client'
7
+ import type { ProtocolVersion } from '@nmtjs/protocol'
8
+ import type { BaseClientFormat } from '@nmtjs/protocol/client'
9
+ import { createFuture } from '@nmtjs/common'
10
+ import { ConnectionType, ProtocolBlob } from '@nmtjs/protocol'
11
+
12
+ type DecodeBase64Function = (data: string) => ArrayBufferView
13
+
14
+ const createDecodeBase64 = (
15
+ customFn?: DecodeBase64Function,
16
+ ): DecodeBase64Function => {
17
+ return (string: string) => {
18
+ if (
19
+ 'fromBase64' in Uint8Array &&
20
+ typeof Uint8Array.fromBase64 === 'function'
21
+ ) {
22
+ return Uint8Array.fromBase64(string)
23
+ } else if (typeof atob === 'function') {
24
+ return Uint8Array.from(atob(string), (c) => c.charCodeAt(0))
25
+ } else if (customFn) {
26
+ return customFn(string)
27
+ } else {
28
+ throw new Error('No base64 decoding function available')
29
+ }
30
+ }
31
+ }
32
+
33
+ const NEEMATA_BLOB_HEADER = 'X-Neemata-Blob'
34
+
35
+ export type HttpClientTransportOptions = {
36
+ /**
37
+ * The origin of the server
38
+ * @example 'http://localhost:3000'
39
+ */
40
+ url: string
41
+ debug?: boolean
42
+ EventSource?: typeof EventSource
43
+ decodeBase64?: DecodeBase64Function
44
+ }
45
+
46
+ export class HttpTransportClient
47
+ implements ClientTransport<ConnectionType.Unidirectional>
48
+ {
49
+ type: ConnectionType.Unidirectional = ConnectionType.Unidirectional
50
+ decodeBase64: DecodeBase64Function
51
+
52
+ constructor(
53
+ protected readonly format: BaseClientFormat,
54
+ protected readonly protocol: ProtocolVersion,
55
+ protected options: HttpClientTransportOptions,
56
+ ) {
57
+ this.options = { debug: false, ...options }
58
+ this.decodeBase64 = createDecodeBase64(options.decodeBase64)
59
+ }
60
+
61
+ url({
62
+ procedure,
63
+ application,
64
+ payload,
65
+ }: {
66
+ procedure: string
67
+ application?: string
68
+ payload?: unknown
69
+ }) {
70
+ const base = application ? `/${application}/${procedure}` : `/${procedure}`
71
+ const url = new URL(base, this.options.url)
72
+ if (payload) url.searchParams.set('payload', JSON.stringify(payload))
73
+ return url
74
+ }
75
+
76
+ async call(
77
+ client: ClientTransportRpcParams,
78
+ rpc: { callId: number; procedure: string; payload: unknown },
79
+ options: ClientTransportMessageOptions,
80
+ ) {
81
+ const { procedure, payload } = rpc
82
+ const requestHeaders = new Headers()
83
+
84
+ const url = this.url({ application: client.application, procedure })
85
+
86
+ if (client.auth) requestHeaders.set('Authorization', client.auth)
87
+
88
+ let body: any
89
+
90
+ if (payload instanceof ProtocolBlob) {
91
+ requestHeaders.set('Content-Type', payload.metadata.type)
92
+ requestHeaders.set(NEEMATA_BLOB_HEADER, 'true')
93
+ } else {
94
+ requestHeaders.set('Content-Type', client.format.contentType)
95
+ const buffer = client.format.encode(payload)
96
+ body = buffer
97
+ }
98
+
99
+ if (options._stream_response) {
100
+ const _constructor = this.options.EventSource
101
+ ? this.options.EventSource
102
+ : EventSource
103
+ const source = new _constructor(url.toString(), { withCredentials: true })
104
+ const future = createFuture<{
105
+ type: 'rpc_stream'
106
+ stream: ReadableStream<ArrayBufferView>
107
+ }>()
108
+ const { readable, writable } = new TransformStream()
109
+ const writer = writable.getWriter()
110
+ source.addEventListener('open', () =>
111
+ future.resolve({ type: 'rpc_stream', stream: readable }),
112
+ )
113
+ source.addEventListener('close', () => writable.close())
114
+ source.addEventListener('error', (event) => {
115
+ const error = new Error('Stream error', { cause: event })
116
+ future.reject(error)
117
+ writable.abort(error)
118
+ })
119
+ source.addEventListener('message', (event) => {
120
+ try {
121
+ const buffer = this.decodeBase64(event.data)
122
+ writer.write(buffer)
123
+ } catch (cause) {
124
+ const error = new Error('Failed to decode stream message', { cause })
125
+ writable.abort(error)
126
+ }
127
+ })
128
+ return future.promise
129
+ } else {
130
+ const response = await fetch(url.toString(), {
131
+ body,
132
+ method: 'POST',
133
+ headers: requestHeaders,
134
+ signal: options.signal,
135
+ credentials: 'include',
136
+ keepalive: true,
137
+ })
138
+
139
+ if (response.ok) {
140
+ const isBlob = !!response.headers.get(NEEMATA_BLOB_HEADER)
141
+ if (isBlob) {
142
+ const contentLength = response.headers.get('content-length')
143
+ const size =
144
+ (contentLength && Number.parseInt(contentLength, 10)) || undefined
145
+ const type =
146
+ response.headers.get('content-type') || 'application/octet-stream'
147
+ const disposition = response.headers.get('content-disposition')
148
+ let filename: string | undefined
149
+ if (disposition) {
150
+ const match = disposition.match(/filename="?([^"]+)"?/)
151
+ if (match) filename = match[1]
152
+ }
153
+ return {
154
+ type: 'blob' as const,
155
+ metadata: { type, size, filename },
156
+ source: body,
157
+ }
158
+ } else {
159
+ return { type: 'rpc' as const, result: await response.bytes() }
160
+ }
161
+ } else {
162
+ const decoded = await response.text()
163
+ // throw new ProtocolError()
164
+ throw new Error()
165
+ }
166
+ }
167
+ }
168
+ }
169
+
170
+ export type HttpTransportFactory = ClientTransportFactory<
171
+ ConnectionType.Unidirectional,
172
+ HttpClientTransportOptions,
173
+ HttpTransportClient
174
+ >
175
+
176
+ export const HttpTransportFactory: HttpTransportFactory = (params, options) => {
177
+ return new HttpTransportClient(params.format, params.protocol, options)
178
+ }