@ethersphere/bee-js 2.1.0 → 3.2.0

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 (160) hide show
  1. package/README.md +3 -3
  2. package/dist/index.browser.min.js +1 -1
  3. package/dist/index.browser.min.js.map +1 -1
  4. package/dist/index.min.js +1 -1
  5. package/dist/index.min.js.map +1 -1
  6. package/dist/src/bee-debug.d.ts +5 -1
  7. package/dist/src/bee.d.ts +23 -46
  8. package/dist/src/modules/chunk.d.ts +2 -10
  9. package/dist/src/modules/debug/status.d.ts +10 -4
  10. package/dist/src/types/debug.d.ts +19 -0
  11. package/dist/src/types/index.d.ts +31 -2
  12. package/dist/src/utils/bytes.d.ts +1 -1
  13. package/dist/src/utils/error.d.ts +21 -2
  14. package/dist/src/utils/expose.d.ts +1 -0
  15. package/dist/src/utils/http.d.ts +7 -9
  16. package/dist/src/utils/stamps.d.ts +12 -0
  17. package/dist/src/utils/type.d.ts +6 -0
  18. package/package.json +28 -27
  19. package/.babelrc.js +0 -33
  20. package/.depcheckrc.json +0 -27
  21. package/.editorconfig +0 -6
  22. package/.eslintignore +0 -1
  23. package/.eslintrc.js +0 -109
  24. package/.gitattributes +0 -1
  25. package/.github/config.yaml +0 -23
  26. package/.github/workflows/check.yaml +0 -57
  27. package/.github/workflows/publish_npmjs.yaml +0 -20
  28. package/.github/workflows/release_github.yaml +0 -20
  29. package/.github/workflows/tests.yaml +0 -128
  30. package/.huskyrc +0 -5
  31. package/.prettierrc +0 -13
  32. package/.vscode/launch.json +0 -49
  33. package/.vscode/settings.json +0 -39
  34. package/CHANGELOG.md +0 -520
  35. package/CODEOWNERS +0 -1
  36. package/CODE_OF_CONDUCT.md +0 -76
  37. package/commitlint.config.js +0 -3
  38. package/dist/src/modules/stamps.d.ts +0 -4
  39. package/jest-puppeteer.config.js +0 -5
  40. package/jest.config.ts +0 -85
  41. package/src/bee-debug.ts +0 -601
  42. package/src/bee.ts +0 -1087
  43. package/src/chunk/bmt.ts +0 -55
  44. package/src/chunk/cac.ts +0 -79
  45. package/src/chunk/serialize.ts +0 -16
  46. package/src/chunk/signer.ts +0 -141
  47. package/src/chunk/soc.ts +0 -190
  48. package/src/chunk/span.ts +0 -33
  49. package/src/feed/index.ts +0 -217
  50. package/src/feed/json.ts +0 -33
  51. package/src/feed/topic.ts +0 -23
  52. package/src/feed/type.ts +0 -12
  53. package/src/index.ts +0 -24
  54. package/src/modules/bytes.ts +0 -70
  55. package/src/modules/bzz.ts +0 -179
  56. package/src/modules/chunk.ts +0 -69
  57. package/src/modules/debug/balance.ts +0 -63
  58. package/src/modules/debug/chequebook.ts +0 -175
  59. package/src/modules/debug/chunk.ts +0 -39
  60. package/src/modules/debug/connectivity.ts +0 -61
  61. package/src/modules/debug/settlements.ts +0 -33
  62. package/src/modules/debug/stamps.ts +0 -96
  63. package/src/modules/debug/states.ts +0 -35
  64. package/src/modules/debug/status.ts +0 -34
  65. package/src/modules/debug/tag.ts +0 -19
  66. package/src/modules/debug/transactions.ts +0 -79
  67. package/src/modules/feed.ts +0 -105
  68. package/src/modules/pinning.ts +0 -74
  69. package/src/modules/pss.ts +0 -49
  70. package/src/modules/soc.ts +0 -40
  71. package/src/modules/stamps.ts +0 -59
  72. package/src/modules/status.ts +0 -13
  73. package/src/modules/stewardship.ts +0 -32
  74. package/src/modules/tag.ts +0 -83
  75. package/src/types/debug.ts +0 -204
  76. package/src/types/index.ts +0 -561
  77. package/src/types/ky-universal/common.d.ts +0 -17
  78. package/src/types/ky-universal/hooks.d.ts +0 -103
  79. package/src/types/ky-universal/index.d.ts +0 -125
  80. package/src/types/ky-universal/options.d.ts +0 -235
  81. package/src/types/ky-universal/response.d.ts +0 -38
  82. package/src/types/ky-universal/retry.d.ts +0 -39
  83. package/src/types/tar-js.d.ts +0 -1
  84. package/src/types/webpack-bundle-analyzer/index.d.ts +0 -13
  85. package/src/utils/bytes.ts +0 -144
  86. package/src/utils/collection.ts +0 -151
  87. package/src/utils/data.browser.ts +0 -68
  88. package/src/utils/data.ts +0 -48
  89. package/src/utils/error.ts +0 -23
  90. package/src/utils/eth.ts +0 -229
  91. package/src/utils/expose.ts +0 -48
  92. package/src/utils/file.ts +0 -40
  93. package/src/utils/hash.ts +0 -16
  94. package/src/utils/headers.ts +0 -63
  95. package/src/utils/hex.ts +0 -186
  96. package/src/utils/http.ts +0 -141
  97. package/src/utils/merge.ts +0 -34
  98. package/src/utils/pss.ts +0 -16
  99. package/src/utils/stream.ts +0 -146
  100. package/src/utils/tar.ts +0 -30
  101. package/src/utils/type.ts +0 -394
  102. package/src/utils/uint64.ts +0 -31
  103. package/src/utils/url.ts +0 -52
  104. package/test/bee.sh +0 -187
  105. package/test/data/1.txt +0 -1
  106. package/test/data/2.txt +0 -0
  107. package/test/data/empty +0 -0
  108. package/test/data/sub/3.txt +0 -1
  109. package/test/data/sub//303/251 +0 -0
  110. package/test/data/sub//360/237/230/216 +0 -0
  111. package/test/integration/bee-class.browser.spec.ts +0 -272
  112. package/test/integration/bee-class.spec.ts +0 -648
  113. package/test/integration/bee-debug-class.spec.ts +0 -114
  114. package/test/integration/chunk/bmt.spec.ts +0 -33
  115. package/test/integration/chunk/cac.spec.ts +0 -27
  116. package/test/integration/chunk/soc.spec.ts +0 -38
  117. package/test/integration/feed/index.spec.ts +0 -84
  118. package/test/integration/modules/bytes.spec.ts +0 -23
  119. package/test/integration/modules/bzz.spec.ts +0 -265
  120. package/test/integration/modules/chunk.spec.ts +0 -29
  121. package/test/integration/modules/debug/balance.spec.ts +0 -70
  122. package/test/integration/modules/debug/chequebook.spec.ts +0 -64
  123. package/test/integration/modules/debug/connectivity.spec.ts +0 -69
  124. package/test/integration/modules/debug/settlements.spec.ts +0 -34
  125. package/test/integration/modules/debug/states.spec.ts +0 -34
  126. package/test/integration/modules/debug/status.spec.ts +0 -65
  127. package/test/integration/modules/debug/tag.spec.ts +0 -27
  128. package/test/integration/modules/debug/transactions.spec.ts +0 -10
  129. package/test/integration/modules/feed.spec.ts +0 -61
  130. package/test/integration/modules/pinning.spec.ts +0 -155
  131. package/test/integration/modules/pss.spec.ts +0 -105
  132. package/test/integration/modules/status.spec.ts +0 -10
  133. package/test/integration/modules/stewardship.spec.ts +0 -32
  134. package/test/integration/modules/tag.spec.ts +0 -51
  135. package/test/test-type-sequencer.js +0 -36
  136. package/test/testpage/testpage.html +0 -20
  137. package/test/tests-setup.ts +0 -94
  138. package/test/unit/assertions.ts +0 -460
  139. package/test/unit/bee-class.spec.ts +0 -976
  140. package/test/unit/bee-debug-class.spec.ts +0 -435
  141. package/test/unit/chunk/bmt.spec.ts +0 -16
  142. package/test/unit/chunk/cac.spec.ts +0 -29
  143. package/test/unit/chunk/serialize.spec.ts +0 -24
  144. package/test/unit/chunk/signer.spec.ts +0 -140
  145. package/test/unit/chunk/soc.spec.ts +0 -25
  146. package/test/unit/chunk/span.spec.ts +0 -38
  147. package/test/unit/feed/json.spec.ts +0 -73
  148. package/test/unit/nock.ts +0 -151
  149. package/test/unit/utils/bytes.spec.ts +0 -32
  150. package/test/unit/utils/collection.spec.ts +0 -22
  151. package/test/unit/utils/eth.spec.ts +0 -224
  152. package/test/unit/utils/hex.spec.ts +0 -115
  153. package/test/unit/utils/stream.spec.ts +0 -30
  154. package/test/unit/utils/type.spec.ts +0 -32
  155. package/test/unit/utils/uint64.spec.ts +0 -102
  156. package/test/utils.ts +0 -426
  157. package/tsconfig.json +0 -18
  158. package/tsconfig.test.json +0 -11
  159. package/typedoc.json +0 -7
  160. package/webpack.config.ts +0 -141
package/src/utils/hex.ts DELETED
@@ -1,186 +0,0 @@
1
- import { Bytes, makeBytes } from './bytes'
2
- import { BrandedType, FlavoredType } from '../types'
3
-
4
- /**
5
- * Nominal type to represent hex strings WITHOUT '0x' prefix.
6
- * For example for 32 bytes hex representation you have to use 64 length.
7
- * TODO: Make Length mandatory: https://github.com/ethersphere/bee-js/issues/208
8
- */
9
- export type HexString<Length extends number = number> = FlavoredType<
10
- string & {
11
- readonly length: Length
12
- },
13
- 'HexString'
14
- >
15
-
16
- /**
17
- * Type for HexString with prefix.
18
- * The main hex type used internally should be non-prefixed HexString
19
- * and therefore this type should be used as least as possible.
20
- * Because of that it does not contain the Length property as the variables
21
- * should be validated and converted to HexString ASAP.
22
- */
23
- export type PrefixedHexString = BrandedType<string, 'PrefixedHexString'>
24
-
25
- /**
26
- * Creates unprefixed hex string from wide range of data.
27
- *
28
- * TODO: Make Length mandatory: https://github.com/ethersphere/bee-js/issues/208
29
- *
30
- * @param input
31
- * @param len of the resulting HexString WITHOUT prefix!
32
- */
33
- export function makeHexString<L extends number>(input: string | number | Uint8Array | unknown, len?: L): HexString<L> {
34
- if (typeof input === 'number') {
35
- return intToHex<L>(input, len)
36
- }
37
-
38
- if (input instanceof Uint8Array) {
39
- return bytesToHex<L>(input, len)
40
- }
41
-
42
- if (typeof input === 'string') {
43
- if (isPrefixedHexString(input)) {
44
- const hex = input.slice(2) as HexString<L>
45
-
46
- if (len && hex.length !== len) {
47
- throw new TypeError(`Length mismatch for valid hex string. Expecting length ${len}: ${hex}`)
48
- }
49
-
50
- return hex
51
- } else {
52
- // We use assertHexString() as there might be more reasons why a string is not valid hex string
53
- // and usage of isHexString() would not give enough information to the user on what is going
54
- // wrong.
55
- assertHexString<L>(input, len)
56
-
57
- return input
58
- }
59
- }
60
-
61
- throw new TypeError('Not HexString compatible type!')
62
- }
63
-
64
- /**
65
- * Converts a hex string to Uint8Array
66
- *
67
- * @param hex string input without 0x prefix!
68
- */
69
- export function hexToBytes<Length extends number, LengthHex extends number = number>(
70
- hex: HexString<LengthHex>,
71
- ): Bytes<Length> {
72
- assertHexString(hex)
73
-
74
- const bytes = makeBytes(hex.length / 2)
75
- for (let i = 0; i < bytes.length; i++) {
76
- const hexByte = hex.substr(i * 2, 2)
77
- bytes[i] = parseInt(hexByte, 16)
78
- }
79
-
80
- return bytes as Bytes<Length>
81
- }
82
-
83
- /**
84
- * Converts array of number or Uint8Array to HexString without prefix.
85
- *
86
- * @param bytes The input array
87
- * @param len The length of the non prefixed HexString
88
- */
89
- export function bytesToHex<Length extends number = number>(bytes: Uint8Array, len?: Length): HexString<Length> {
90
- const hexByte = (n: number) => n.toString(16).padStart(2, '0')
91
- const hex = Array.from(bytes, hexByte).join('') as HexString<Length>
92
-
93
- // TODO: Make Length mandatory: https://github.com/ethersphere/bee-js/issues/208
94
- if (len && hex.length !== len) {
95
- throw new TypeError(`Resulting HexString does not have expected length ${len}: ${hex}`)
96
- }
97
-
98
- return hex
99
- }
100
-
101
- /**
102
- * Converts integer number to hex string.
103
- *
104
- * Optionally provides '0x' prefix or padding
105
- *
106
- * @param int The positive integer to be converted
107
- * @param len The length of the non prefixed HexString
108
- */
109
- export function intToHex<Length extends number = number>(int: number, len?: Length): HexString<Length> {
110
- if (!Number.isInteger(int)) throw new TypeError('the value provided is not integer')
111
-
112
- if (int > Number.MAX_SAFE_INTEGER) throw new TypeError('the value provided exceeds safe integer')
113
-
114
- if (int < 0) throw new TypeError('the value provided is a negative integer')
115
- const hex = int.toString(16) as HexString<Length>
116
-
117
- // TODO: Make Length mandatory: https://github.com/ethersphere/bee-js/issues/208
118
- if (len && hex.length !== len) {
119
- throw new TypeError(`Resulting HexString does not have expected length ${len}: ${hex}`)
120
- }
121
-
122
- return hex
123
- }
124
-
125
- /**
126
- * Type guard for HexStrings.
127
- * Requires no 0x prefix!
128
- *
129
- * TODO: Make Length mandatory: https://github.com/ethersphere/bee-js/issues/208
130
- *
131
- * @param s string input
132
- * @param len expected length of the HexString
133
- */
134
- export function isHexString<Length extends number = number>(s: unknown, len?: number): s is HexString<Length> {
135
- return typeof s === 'string' && /^[0-9a-f]+$/i.test(s) && (!len || s.length === len)
136
- }
137
-
138
- /**
139
- * Type guard for PrefixedHexStrings.
140
- * Does enforce presence of 0x prefix!
141
- *
142
- * @param s string input
143
- */
144
- export function isPrefixedHexString(s: unknown): s is PrefixedHexString {
145
- return typeof s === 'string' && /^0x[0-9a-f]+$/i.test(s)
146
- }
147
-
148
- /**
149
- * Verifies if the provided input is a HexString.
150
- *
151
- * TODO: Make Length mandatory: https://github.com/ethersphere/bee-js/issues/208
152
- *
153
- * @param s string input
154
- * @param len expected length of the HexString
155
- * @param name optional name for the asserted value
156
- * @returns HexString or throws error
157
- */
158
- export function assertHexString<Length extends number = number>(
159
- s: unknown,
160
- len?: number,
161
- name = 'value',
162
- ): asserts s is HexString<Length> {
163
- if (!isHexString(s, len)) {
164
- if (isPrefixedHexString(s)) {
165
- throw new TypeError(`${name} not valid non prefixed hex string (has 0x prefix): ${s}`)
166
- }
167
-
168
- // Don't display length error if no length specified in order not to confuse user
169
- const lengthMsg = len ? ` of length ${len}` : ''
170
- throw new TypeError(`${name} not valid hex string${lengthMsg}: ${s}`)
171
- }
172
- }
173
-
174
- /**
175
- * Verifies if the provided input is a PrefixedHexString.
176
- *
177
- * @param s string input
178
- * @param len expected length of the HexString
179
- * @param name optional name for the asserted value
180
- * @returns HexString or throws error
181
- */
182
- export function assertPrefixedHexString(s: string, name = 'value'): asserts s is PrefixedHexString {
183
- if (!isPrefixedHexString(s)) {
184
- throw new TypeError(`${name} not valid prefixed hex string: ${s}`)
185
- }
186
- }
package/src/utils/http.ts DELETED
@@ -1,141 +0,0 @@
1
- import { BeeError, BeeRequestError, BeeResponseError } from './error'
2
- import type { BeeRequest, BeeResponse, HookCallback, HttpMethod, Ky } from '../types'
3
- import kyFactory, { Options as KyOptions } from 'ky-universal'
4
- import { normalizeToReadableStream } from './stream'
5
- import { deepMerge } from './merge'
6
- import { version as beeJsVersion } from '../../package.json'
7
- import { isStrictlyObject } from './type'
8
-
9
- const DEFAULT_KY_CONFIG: KyOptions = {
10
- headers: {
11
- accept: 'application/json, text/plain, */*',
12
- 'user-agent': `bee-js/${beeJsVersion}`,
13
- },
14
- }
15
-
16
- interface HttpOptions extends Omit<KyOptions, 'searchParams'> {
17
- path: string
18
- responseType?: 'json' | 'arraybuffer' | 'stream'
19
-
20
- /**
21
- * Overridden parameter that allows undefined as a value.
22
- */
23
- searchParams?: Record<string, string | number | boolean | undefined>
24
- }
25
-
26
- interface KyResponse<T> extends Response {
27
- data: T
28
- }
29
-
30
- function headersToObject(header: Headers) {
31
- return [...header.entries()].reduce<Record<string, string>>((obj, [key, val]) => {
32
- obj[key] = val
33
-
34
- return obj
35
- }, {})
36
- }
37
-
38
- function wrapRequest(request: Request): BeeRequest {
39
- return {
40
- url: request.url,
41
- method: request.method.toUpperCase() as HttpMethod,
42
- headers: headersToObject(request.headers),
43
- }
44
- }
45
-
46
- export function wrapRequestClosure(cb: HookCallback<BeeRequest>): (request: Request) => Promise<void> {
47
- return async (request: Request) => {
48
- await cb(wrapRequest(request))
49
- }
50
- }
51
-
52
- export function wrapResponseClosure(
53
- cb: HookCallback<BeeResponse>,
54
- ): (request: Request, options: unknown, response: Response) => Promise<void> {
55
- return async (request: Request, options: unknown, response: Response) => {
56
- await cb({
57
- headers: headersToObject(response.headers),
58
- status: response.status,
59
- statusText: response.statusText,
60
- request: wrapRequest(request),
61
- })
62
- }
63
- }
64
-
65
- /**
66
- * Filters out entries that has undefined value from headers object.
67
- * Modifies the original object!
68
- *
69
- * @param obj
70
- */
71
- // eslint-disable-next-line @typescript-eslint/ban-types
72
- export function filterHeaders(obj?: object): Record<string, string> | undefined {
73
- if (obj === undefined) {
74
- return undefined
75
- }
76
-
77
- isStrictlyObject(obj)
78
-
79
- const typedObj = obj as Record<string, string>
80
-
81
- for (const key in typedObj) {
82
- if (typedObj[key] === undefined) {
83
- delete typedObj[key]
84
- }
85
- }
86
-
87
- if (Object.keys(typedObj).length === 0) {
88
- return undefined
89
- }
90
-
91
- return typedObj
92
- }
93
-
94
- export async function http<T>(ky: Ky, config: HttpOptions): Promise<KyResponse<T>> {
95
- try {
96
- const { path, responseType, ...kyConfig } = config
97
-
98
- const response = (await ky(path, {
99
- ...kyConfig,
100
- searchParams: filterHeaders(kyConfig.searchParams),
101
- })) as KyResponse<T>
102
-
103
- switch (responseType) {
104
- case 'stream':
105
- if (!response.body) {
106
- throw new BeeError('Response was expected to get data but did not get any!')
107
- }
108
-
109
- response.data = normalizeToReadableStream(response.body) as unknown as T
110
- break
111
- case 'arraybuffer':
112
- response.data = (await response.arrayBuffer()) as unknown as T
113
- break
114
- case 'json':
115
- response.data = (await response.json()) as unknown as T
116
- break
117
- default:
118
- break // If responseType is not set, then no data are expected
119
- }
120
-
121
- return response
122
- } catch (e) {
123
- if (e.response) {
124
- const message = (await e.response.json()).message
125
-
126
- if (message) {
127
- throw new BeeResponseError(e.response.status, `${e.response.statusText}: ${message}`)
128
- } else {
129
- throw new BeeResponseError(e.response.status, e.response.statusText)
130
- }
131
- } else if (e.request) {
132
- throw new BeeRequestError(e.message)
133
- } else {
134
- throw new BeeError(e.message)
135
- }
136
- }
137
- }
138
-
139
- export function makeDefaultKy(kyConfig: KyOptions): Ky {
140
- return kyFactory.create(deepMerge(DEFAULT_KY_CONFIG, kyConfig))
141
- }
@@ -1,34 +0,0 @@
1
- import { isObject } from './type'
2
-
3
- /**
4
- * Function that deep merges objects
5
- *
6
- * @copyright https://github.com/sindresorhus/ky/blob/b3c9e88fa49d50150dbb6e6b771b4af56cb40c98/source/utils/merge.ts
7
- * @licence MIT
8
- * @param sources
9
- */
10
- export function deepMerge<T>(...sources: Array<Partial<T> | undefined>): T {
11
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
12
- let returnValue: any = {}
13
-
14
- for (const source of sources) {
15
- if (Array.isArray(source)) {
16
- if (!Array.isArray(returnValue)) {
17
- returnValue = []
18
- }
19
-
20
- returnValue = [...returnValue, ...source]
21
- } else if (isObject(source)) {
22
- // eslint-disable-next-line prefer-const
23
- for (let [key, value] of Object.entries(source)) {
24
- if (isObject(value) && key in returnValue) {
25
- value = deepMerge(returnValue[key], value)
26
- }
27
-
28
- returnValue = { ...returnValue, [key]: value }
29
- }
30
- }
31
- }
32
-
33
- return returnValue
34
- }
package/src/utils/pss.ts DELETED
@@ -1,16 +0,0 @@
1
- import { AddressPrefix, PSS_TARGET_HEX_LENGTH_MAX } from '../types'
2
-
3
- /**
4
- * Utility function that for given strings/reference takes the most specific
5
- * target that Bee node will except.
6
- *
7
- * @param target is a non-prefixed hex string Bee address
8
- * @see [Bee docs - PSS](https://docs.ethswarm.org/docs/dapps-on-swarm/pss)
9
- */
10
- export function makeMaxTarget(target: string): AddressPrefix {
11
- if (typeof target !== 'string') {
12
- throw new TypeError('target has to be an string!')
13
- }
14
-
15
- return target.slice(0, PSS_TARGET_HEX_LENGTH_MAX)
16
- }
@@ -1,146 +0,0 @@
1
- import type { Readable as NodeReadableType } from 'stream'
2
- import { isStrictlyObject } from './type'
3
- import { ReadableStream } from 'web-streams-polyfill/ponyfill'
4
- import { Readable as NodeReadable, ReadableOptions as NodeReadableOptions } from 'readable-stream'
5
-
6
- import { Readable } from '../types'
7
-
8
- /**
9
- * Validates if passed object is either browser's ReadableStream
10
- * or Node's Readable.
11
- *
12
- * @param entry
13
- */
14
- export function isReadable(entry: unknown): entry is NodeReadableType {
15
- return isReadableStream(entry) || isNodeReadable(entry)
16
- }
17
-
18
- export function isReadableStream(entry: unknown): entry is ReadableStream {
19
- if (!isStrictlyObject(entry)) {
20
- return false
21
- }
22
-
23
- const browserReadable = entry as ReadableStream
24
-
25
- if (
26
- typeof browserReadable.getReader === 'function' &&
27
- browserReadable.locked !== undefined &&
28
- typeof browserReadable.cancel === 'function' &&
29
- typeof browserReadable.pipeTo === 'function' &&
30
- typeof browserReadable.pipeThrough === 'function'
31
- ) {
32
- return true
33
- }
34
-
35
- return false
36
- }
37
-
38
- export function isNodeReadable(entry: unknown): entry is NodeReadableType {
39
- if (!isStrictlyObject(entry)) {
40
- return false
41
- }
42
-
43
- const nodeReadable = entry as NodeReadableType
44
-
45
- if (typeof nodeReadable.pipe === 'function' && nodeReadable.readable && typeof nodeReadable._read === 'function') {
46
- return true
47
- }
48
-
49
- return false
50
- }
51
-
52
- /**
53
- * Function that converts Node's Readable into WHATWG ReadableStream
54
- *
55
- * Taken over from https://github.com/gwicke/node-web-streams/blob/master/lib/conversions.js
56
- * Because it uses forked web-streams-polyfill that are outdated.
57
- *
58
- * @author https://github.com/gwicke
59
- * @licence Apache License 2.0 https://github.com/gwicke/node-web-streams/blob/master/LICENSE
60
- * @param nodeStream
61
- */
62
- export function readableNodeToWeb(nodeStream: NodeReadableType): ReadableStream<Uint8Array> {
63
- return new ReadableStream({
64
- start(controller) {
65
- nodeStream.pause()
66
- nodeStream.on('data', chunk => {
67
- controller.enqueue(chunk)
68
- nodeStream.pause()
69
- })
70
- nodeStream.on('end', () => controller.close())
71
- nodeStream.on('error', e => controller.error(e))
72
- },
73
- pull() {
74
- nodeStream.resume()
75
- },
76
- cancel() {
77
- nodeStream.pause()
78
- },
79
- })
80
- }
81
-
82
- /**
83
- * Taken over from https://github.com/gwicke/node-web-streams/blob/master/lib/conversions.js
84
- * Because it uses forked web-streams-polyfill that are outdated.
85
- *
86
- * @author https://github.com/gwicke
87
- * @licence Apache License 2.0 https://github.com/gwicke/node-web-streams/blob/master/LICENSE
88
- */
89
- class NodeReadableWrapper extends NodeReadable {
90
- private _webStream: ReadableStream
91
- private _reader: ReadableStreamDefaultReader<any>
92
- private _reading: boolean
93
- constructor(webStream: ReadableStream, options?: NodeReadableOptions) {
94
- super(options)
95
- this._webStream = webStream
96
- this._reader = webStream.getReader()
97
- this._reading = false
98
- }
99
-
100
- _read() {
101
- if (this._reading) {
102
- return
103
- }
104
- this._reading = true
105
- const doRead = () => {
106
- this._reader.read().then(res => {
107
- if (res.done) {
108
- this.push(null)
109
-
110
- return
111
- }
112
-
113
- if (this.push(res.value)) {
114
- return doRead()
115
- } else {
116
- this._reading = false
117
- }
118
- })
119
- }
120
- doRead()
121
- }
122
- }
123
-
124
- /**
125
- * Function that converts WHATWG ReadableStream into Node's Readable
126
- *
127
- * Taken over from https://github.com/gwicke/node-web-streams/blob/master/lib/conversions.js
128
- * Because it uses forked web-streams-polyfill that are outdated.
129
- *
130
- * @author https://github.com/gwicke
131
- * @licence Apache License 2.0 https://github.com/gwicke/node-web-streams/blob/master/LICENSE
132
- * @param webStream
133
- */
134
- export function readableWebToNode(webStream: ReadableStream, options?: NodeReadableOptions): NodeReadableType {
135
- return new NodeReadableWrapper(webStream, options) as unknown as NodeReadableType
136
- }
137
-
138
- export function normalizeToReadableStream(stream: Readable): ReadableStream {
139
- if (isNodeReadable(stream)) {
140
- return readableNodeToWeb(stream)
141
- } else if (isReadableStream(stream)) {
142
- return stream
143
- }
144
-
145
- throw new TypeError('Passed stream is not Node Readable nor ReadableStream!')
146
- }
package/src/utils/tar.ts DELETED
@@ -1,30 +0,0 @@
1
- import { Collection } from '../types'
2
- import Tar from 'tar-js'
3
-
4
- // this is a workaround type so that we are able to pass in Uint8Arrays
5
- // as string to `tar.append`
6
- interface StringLike {
7
- readonly length: number
8
- charCodeAt: (index: number) => number
9
- }
10
-
11
- // converts a string to utf8 Uint8Array and returns it as a string-like
12
- // object that `tar.append` accepts as path
13
- function fixUnicodePath(path: string): StringLike {
14
- const codes = new TextEncoder().encode(path)
15
-
16
- return {
17
- length: codes.length,
18
- charCodeAt: index => codes[index],
19
- }
20
- }
21
-
22
- export function makeTar(data: Collection<Uint8Array>): Uint8Array {
23
- const tar = new Tar()
24
- for (const entry of data) {
25
- const path = fixUnicodePath(entry.path)
26
- tar.append(path, entry.data)
27
- }
28
-
29
- return tar.out
30
- }