@juzi/wechaty-puppet-service 1.0.42 → 1.0.44

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 (128) hide show
  1. package/dist/cjs/src/client/puppet-service.d.ts +1 -4
  2. package/dist/cjs/src/client/puppet-service.d.ts.map +1 -1
  3. package/dist/cjs/src/client/puppet-service.js +47 -129
  4. package/dist/cjs/src/client/puppet-service.js.map +1 -1
  5. package/dist/cjs/src/package-json.js +3 -3
  6. package/dist/cjs/src/server/puppet-implementation.d.ts.map +1 -1
  7. package/dist/cjs/src/server/puppet-implementation.js +14 -66
  8. package/dist/cjs/src/server/puppet-implementation.js.map +1 -1
  9. package/dist/esm/src/client/puppet-service.d.ts +1 -4
  10. package/dist/esm/src/client/puppet-service.d.ts.map +1 -1
  11. package/dist/esm/src/client/puppet-service.js +44 -126
  12. package/dist/esm/src/client/puppet-service.js.map +1 -1
  13. package/dist/esm/src/package-json.js +3 -3
  14. package/dist/esm/src/server/puppet-implementation.d.ts.map +1 -1
  15. package/dist/esm/src/server/puppet-implementation.js +12 -64
  16. package/dist/esm/src/server/puppet-implementation.js.map +1 -1
  17. package/package.json +3 -3
  18. package/src/client/puppet-service.ts +54 -148
  19. package/src/package-json.ts +3 -3
  20. package/src/server/puppet-implementation.ts +16 -82
  21. package/dist/cjs/src/deprecated/chunk-pb.d.ts +0 -15
  22. package/dist/cjs/src/deprecated/chunk-pb.d.ts.map +0 -1
  23. package/dist/cjs/src/deprecated/chunk-pb.js +0 -60
  24. package/dist/cjs/src/deprecated/chunk-pb.js.map +0 -1
  25. package/dist/cjs/src/deprecated/chunk-pb.spec.d.ts +0 -3
  26. package/dist/cjs/src/deprecated/chunk-pb.spec.d.ts.map +0 -1
  27. package/dist/cjs/src/deprecated/chunk-pb.spec.js +0 -144
  28. package/dist/cjs/src/deprecated/chunk-pb.spec.js.map +0 -1
  29. package/dist/cjs/src/deprecated/conversation-id-file-box.d.ts +0 -21
  30. package/dist/cjs/src/deprecated/conversation-id-file-box.d.ts.map +0 -1
  31. package/dist/cjs/src/deprecated/conversation-id-file-box.js +0 -43
  32. package/dist/cjs/src/deprecated/conversation-id-file-box.js.map +0 -1
  33. package/dist/cjs/src/deprecated/conversation-id-file-box.spec.d.ts +0 -3
  34. package/dist/cjs/src/deprecated/conversation-id-file-box.spec.d.ts.map +0 -1
  35. package/dist/cjs/src/deprecated/conversation-id-file-box.spec.js +0 -94
  36. package/dist/cjs/src/deprecated/conversation-id-file-box.spec.js.map +0 -1
  37. package/dist/cjs/src/deprecated/file-box-chunk.d.ts +0 -14
  38. package/dist/cjs/src/deprecated/file-box-chunk.d.ts.map +0 -1
  39. package/dist/cjs/src/deprecated/file-box-chunk.js +0 -67
  40. package/dist/cjs/src/deprecated/file-box-chunk.js.map +0 -1
  41. package/dist/cjs/src/deprecated/file-box-chunk.spec.d.ts +0 -3
  42. package/dist/cjs/src/deprecated/file-box-chunk.spec.d.ts.map +0 -1
  43. package/dist/cjs/src/deprecated/file-box-chunk.spec.js +0 -134
  44. package/dist/cjs/src/deprecated/file-box-chunk.spec.js.map +0 -1
  45. package/dist/cjs/src/deprecated/file-box-pb.d.ts +0 -15
  46. package/dist/cjs/src/deprecated/file-box-pb.d.ts.map +0 -1
  47. package/dist/cjs/src/deprecated/file-box-pb.js +0 -26
  48. package/dist/cjs/src/deprecated/file-box-pb.js.map +0 -1
  49. package/dist/cjs/src/deprecated/file-box-pb.spec.d.ts +0 -3
  50. package/dist/cjs/src/deprecated/file-box-pb.spec.d.ts.map +0 -1
  51. package/dist/cjs/src/deprecated/file-box-pb.spec.js +0 -18
  52. package/dist/cjs/src/deprecated/file-box-pb.spec.js.map +0 -1
  53. package/dist/cjs/src/deprecated/file-box-pb.type.d.ts +0 -15
  54. package/dist/cjs/src/deprecated/file-box-pb.type.d.ts.map +0 -1
  55. package/dist/cjs/src/deprecated/file-box-pb.type.js +0 -3
  56. package/dist/cjs/src/deprecated/file-box-pb.type.js.map +0 -1
  57. package/dist/cjs/src/deprecated/mod.d.ts +0 -3
  58. package/dist/cjs/src/deprecated/mod.d.ts.map +0 -1
  59. package/dist/cjs/src/deprecated/mod.js +0 -10
  60. package/dist/cjs/src/deprecated/mod.js.map +0 -1
  61. package/dist/cjs/src/deprecated/next-data.d.ts +0 -7
  62. package/dist/cjs/src/deprecated/next-data.d.ts.map +0 -1
  63. package/dist/cjs/src/deprecated/next-data.js +0 -26
  64. package/dist/cjs/src/deprecated/next-data.js.map +0 -1
  65. package/dist/cjs/src/deprecated/serialize-file-box.d.ts +0 -6
  66. package/dist/cjs/src/deprecated/serialize-file-box.d.ts.map +0 -1
  67. package/dist/cjs/src/deprecated/serialize-file-box.js +0 -22
  68. package/dist/cjs/src/deprecated/serialize-file-box.js.map +0 -1
  69. package/dist/esm/src/deprecated/chunk-pb.d.ts +0 -15
  70. package/dist/esm/src/deprecated/chunk-pb.d.ts.map +0 -1
  71. package/dist/esm/src/deprecated/chunk-pb.js +0 -56
  72. package/dist/esm/src/deprecated/chunk-pb.js.map +0 -1
  73. package/dist/esm/src/deprecated/chunk-pb.spec.d.ts +0 -3
  74. package/dist/esm/src/deprecated/chunk-pb.spec.d.ts.map +0 -1
  75. package/dist/esm/src/deprecated/chunk-pb.spec.js +0 -142
  76. package/dist/esm/src/deprecated/chunk-pb.spec.js.map +0 -1
  77. package/dist/esm/src/deprecated/conversation-id-file-box.d.ts +0 -21
  78. package/dist/esm/src/deprecated/conversation-id-file-box.d.ts.map +0 -1
  79. package/dist/esm/src/deprecated/conversation-id-file-box.js +0 -39
  80. package/dist/esm/src/deprecated/conversation-id-file-box.js.map +0 -1
  81. package/dist/esm/src/deprecated/conversation-id-file-box.spec.d.ts +0 -3
  82. package/dist/esm/src/deprecated/conversation-id-file-box.spec.d.ts.map +0 -1
  83. package/dist/esm/src/deprecated/conversation-id-file-box.spec.js +0 -92
  84. package/dist/esm/src/deprecated/conversation-id-file-box.spec.js.map +0 -1
  85. package/dist/esm/src/deprecated/file-box-chunk.d.ts +0 -14
  86. package/dist/esm/src/deprecated/file-box-chunk.d.ts.map +0 -1
  87. package/dist/esm/src/deprecated/file-box-chunk.js +0 -63
  88. package/dist/esm/src/deprecated/file-box-chunk.js.map +0 -1
  89. package/dist/esm/src/deprecated/file-box-chunk.spec.d.ts +0 -3
  90. package/dist/esm/src/deprecated/file-box-chunk.spec.d.ts.map +0 -1
  91. package/dist/esm/src/deprecated/file-box-chunk.spec.js +0 -132
  92. package/dist/esm/src/deprecated/file-box-chunk.spec.js.map +0 -1
  93. package/dist/esm/src/deprecated/file-box-pb.d.ts +0 -15
  94. package/dist/esm/src/deprecated/file-box-pb.d.ts.map +0 -1
  95. package/dist/esm/src/deprecated/file-box-pb.js +0 -22
  96. package/dist/esm/src/deprecated/file-box-pb.js.map +0 -1
  97. package/dist/esm/src/deprecated/file-box-pb.spec.d.ts +0 -3
  98. package/dist/esm/src/deprecated/file-box-pb.spec.d.ts.map +0 -1
  99. package/dist/esm/src/deprecated/file-box-pb.spec.js +0 -16
  100. package/dist/esm/src/deprecated/file-box-pb.spec.js.map +0 -1
  101. package/dist/esm/src/deprecated/file-box-pb.type.d.ts +0 -15
  102. package/dist/esm/src/deprecated/file-box-pb.type.d.ts.map +0 -1
  103. package/dist/esm/src/deprecated/file-box-pb.type.js +0 -2
  104. package/dist/esm/src/deprecated/file-box-pb.type.js.map +0 -1
  105. package/dist/esm/src/deprecated/mod.d.ts +0 -3
  106. package/dist/esm/src/deprecated/mod.d.ts.map +0 -1
  107. package/dist/esm/src/deprecated/mod.js +0 -3
  108. package/dist/esm/src/deprecated/mod.js.map +0 -1
  109. package/dist/esm/src/deprecated/next-data.d.ts +0 -7
  110. package/dist/esm/src/deprecated/next-data.d.ts.map +0 -1
  111. package/dist/esm/src/deprecated/next-data.js +0 -23
  112. package/dist/esm/src/deprecated/next-data.js.map +0 -1
  113. package/dist/esm/src/deprecated/serialize-file-box.d.ts +0 -6
  114. package/dist/esm/src/deprecated/serialize-file-box.d.ts.map +0 -1
  115. package/dist/esm/src/deprecated/serialize-file-box.js +0 -18
  116. package/dist/esm/src/deprecated/serialize-file-box.js.map +0 -1
  117. package/src/deprecated/chunk-pb.spec.ts +0 -210
  118. package/src/deprecated/chunk-pb.ts +0 -75
  119. package/src/deprecated/conversation-id-file-box.spec.ts +0 -131
  120. package/src/deprecated/conversation-id-file-box.ts +0 -72
  121. package/src/deprecated/file-box-chunk.spec.ts +0 -184
  122. package/src/deprecated/file-box-chunk.ts +0 -87
  123. package/src/deprecated/file-box-pb.spec.ts +0 -31
  124. package/src/deprecated/file-box-pb.ts +0 -41
  125. package/src/deprecated/file-box-pb.type.ts +0 -16
  126. package/src/deprecated/mod.ts +0 -8
  127. package/src/deprecated/next-data.ts +0 -35
  128. package/src/deprecated/serialize-file-box.ts +0 -24
@@ -1,131 +0,0 @@
1
- #!/usr/bin/env -S node --no-warnings --loader ts-node/esm
2
-
3
- /**
4
- * Wechaty Chatbot SDK - https://github.com/wechaty/wechaty
5
- *
6
- * @copyright 2016 Huan LI (李卓桓) <https://github.com/huan>, and
7
- * Wechaty Contributors <https://github.com/wechaty>.
8
- *
9
- * Licensed under the Apache License, Version 2.0 (the "License");
10
- * you may not use this file except in compliance with the License.
11
- * You may obtain a copy of the License at
12
- *
13
- * http://www.apache.org/licenses/LICENSE-2.0
14
- *
15
- * Unless required by applicable law or agreed to in writing, software
16
- * distributed under the License is distributed on an "AS IS" BASIS,
17
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18
- * See the License for the specific language governing permissions and
19
- * limitations under the License.
20
- *
21
- */
22
- import {
23
- puppet,
24
- } from '@juzi/wechaty-grpc'
25
-
26
- import { test } from 'tstest'
27
- import { PassThrough } from 'stream'
28
- import { FileBox } from 'file-box'
29
-
30
- import { nextData } from './next-data.js'
31
- import {
32
- packConversationIdFileBoxToPb,
33
- unpackConversationIdFileBoxArgsFromPb,
34
- } from './conversation-id-file-box.js'
35
-
36
- test('unpackConversationIdFileBoxArgsFromPb()', async t => {
37
- const FILE_BOX_DATA = 'test'
38
- const FILE_BOX_NAME = 'test.dat'
39
- const CONVERSATION_ID = 'conversation_id'
40
-
41
- const fileBox = FileBox.fromBuffer(
42
- Buffer.from(FILE_BOX_DATA),
43
- FILE_BOX_NAME,
44
- )
45
-
46
- const stream = new PassThrough({ objectMode: true })
47
-
48
- const req1 = new puppet.MessageSendFileStreamRequest()
49
- req1.setConversationId(CONVERSATION_ID)
50
- stream.write(req1)
51
-
52
- const req2 = new puppet.MessageSendFileStreamRequest()
53
- const chunk1 = new puppet.FileBoxChunk()
54
- chunk1.setName(fileBox.name)
55
- req2.setFileBoxChunk(chunk1)
56
- stream.write(req2)
57
-
58
- const fileBoxStream = await fileBox.toStream()
59
- fileBoxStream.on('data', chunk => {
60
- const fileBoxChunk = new puppet.FileBoxChunk()
61
- fileBoxChunk.setData(chunk)
62
- const req3 = new puppet.MessageSendFileStreamRequest()
63
- req3.setFileBoxChunk(fileBoxChunk)
64
- stream.write(req3)
65
- })
66
- fileBoxStream.on('end', () => stream.end())
67
-
68
- const args = await unpackConversationIdFileBoxArgsFromPb(stream)
69
- const data = (await args.fileBox.toBuffer()).toString()
70
-
71
- t.equal(args.conversationId, CONVERSATION_ID, 'should get conversation id')
72
- t.equal(args.fileBox.name, FILE_BOX_NAME, 'should get file box name')
73
- t.equal(data, FILE_BOX_DATA, 'should get file box data')
74
- })
75
-
76
- test('packConversationIdFileBoxToPb()', async t => {
77
- const FILE_BOX_DATA = 'test'
78
- const FILE_BOX_NAME = 'test.dat'
79
- const CONVERSATION_ID = 'conv_id'
80
-
81
- const fileBox = FileBox.fromBuffer(
82
- Buffer.from(FILE_BOX_DATA),
83
- FILE_BOX_NAME,
84
- )
85
-
86
- const stream = await packConversationIdFileBoxToPb(puppet.MessageSendFileStreamRequest)(
87
- CONVERSATION_ID,
88
- fileBox,
89
- )
90
-
91
- const data1 = await nextData(stream)
92
- t.equal(data1.getConversationId(), CONVERSATION_ID, 'match conversation id')
93
-
94
- const data2 = await nextData(stream)
95
- t.ok(data2.hasFileBoxChunk(), 'has file box chunk')
96
- t.ok(data2.getFileBoxChunk()!.hasName(), 'has file box name')
97
- t.equal(data2.getFileBoxChunk()!.getName(), FILE_BOX_NAME, 'match file box name')
98
-
99
- let data = ''
100
- stream.on('data', (chunk: puppet.MessageSendFileStreamRequest) => {
101
- if (!chunk.hasFileBoxChunk()) {
102
- throw new Error('no file box chunk')
103
- }
104
- if (!chunk.getFileBoxChunk()!.hasData()) {
105
- throw new Error('no file box chunk data')
106
- }
107
- data += chunk.getFileBoxChunk()!.getData()
108
- })
109
-
110
- await new Promise(resolve => stream.on('end', resolve))
111
-
112
- t.equal(data.toString(), FILE_BOX_DATA, 'should get file box data')
113
- })
114
-
115
- test('unpackConversationIdFileBoxArgsFromPb() <-> packConversationIdFileBoxToPb()', async t => {
116
- const FILE_BOX_DATA = 'test'
117
- const FILE_BOX_NAME = 'test.dat'
118
- const CONVERSATION_ID = 'conv_id'
119
-
120
- const fileBox = FileBox.fromBuffer(
121
- Buffer.from(FILE_BOX_DATA),
122
- FILE_BOX_NAME,
123
- )
124
-
125
- const stream = await packConversationIdFileBoxToPb(puppet.MessageSendFileStreamRequest)(CONVERSATION_ID, fileBox)
126
- const args = await unpackConversationIdFileBoxArgsFromPb(stream)
127
-
128
- t.equal(args.conversationId, CONVERSATION_ID, 'should match conversation id')
129
- t.equal(args.fileBox.name, FILE_BOX_NAME, 'should be same name')
130
- t.equal((await args.fileBox.toBuffer()).toString(), FILE_BOX_DATA, 'should be same content')
131
- })
@@ -1,72 +0,0 @@
1
- import type {
2
- FileBoxInterface,
3
- } from 'file-box'
4
- import { PassThrough } from 'stream'
5
- import type { Readable } from 'stronger-typed-streams'
6
-
7
- import { nextData } from './next-data.js'
8
- import {
9
- packFileBoxToPb,
10
- unpackFileBoxFromPb,
11
- } from './file-box-pb.js'
12
- import type { ConversationIdFileBoxPb } from './file-box-pb.type.js'
13
-
14
- interface ConversationIdFileBoxArgs {
15
- conversationId: string,
16
- fileBox: FileBoxInterface,
17
- }
18
-
19
- /**
20
- * MessageSendFileStreamRequest to Args
21
- * @deprecated Will be removed after Dec 31, 2022
22
- */
23
- async function unpackConversationIdFileBoxArgsFromPb (
24
- stream: Readable<ConversationIdFileBoxPb>,
25
- ): Promise<ConversationIdFileBoxArgs> {
26
- const chunk = await nextData(stream)
27
- if (!chunk.hasConversationId()) {
28
- throw new Error('no conversation id')
29
- }
30
- const conversationId = chunk.getConversationId()
31
-
32
- // unpackFileBoxFromChunk(unpackFileBoxChunkFromPb(stream))
33
- const fileBox = await unpackFileBoxFromPb(stream)
34
-
35
- return {
36
- conversationId,
37
- fileBox,
38
- }
39
- }
40
-
41
- /**
42
- * Args to MessageSendFileStreamRequest
43
- * @deprecated Will be removed after Dec 31, 2022
44
- */
45
- function packConversationIdFileBoxToPb<T extends ConversationIdFileBoxPb> (
46
- PbConstructor: { new(): T },
47
- ) {
48
- return async (
49
- conversationId: string,
50
- fileBox: FileBoxInterface,
51
- ): Promise<
52
- Readable<T>
53
- > => {
54
- const stream = new PassThrough({ objectMode: true })
55
-
56
- const first = new PbConstructor()
57
- first.setConversationId(conversationId)
58
- stream.write(first)
59
-
60
- // const fileBoxChunkStream = await packFileBoxToChunk(fileBox)
61
- // packFileBoxChunkToPb(MessageSendFileStreamRequest)(fileBoxChunkStream)
62
- const pbStream = await packFileBoxToPb(PbConstructor)(fileBox)
63
- pbStream.pipe(stream)
64
-
65
- return stream
66
- }
67
- }
68
-
69
- export {
70
- unpackConversationIdFileBoxArgsFromPb,
71
- packConversationIdFileBoxToPb,
72
- }
@@ -1,184 +0,0 @@
1
- #!/usr/bin/env -S node --no-warnings --loader ts-node/esm
2
-
3
- /**
4
- * Wechaty Chatbot SDK - https://github.com/wechaty/wechaty
5
- *
6
- * @copyright 2016 Huan LI (李卓桓) <https://github.com/huan>, and
7
- * Wechaty Contributors <https://github.com/wechaty>.
8
- *
9
- * Licensed under the Apache License, Version 2.0 (the "License");
10
- * you may not use this file except in compliance with the License.
11
- * You may obtain a copy of the License at
12
- *
13
- * http://www.apache.org/licenses/LICENSE-2.0
14
- *
15
- * Unless required by applicable law or agreed to in writing, software
16
- * distributed under the License is distributed on an "AS IS" BASIS,
17
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18
- * See the License for the specific language governing permissions and
19
- * limitations under the License.
20
- *
21
- */
22
- import { test } from 'tstest'
23
-
24
- import { PassThrough } from 'stream'
25
- import type { Duplex } from 'stronger-typed-streams'
26
- import { FileBox } from 'file-box'
27
-
28
- import {
29
- puppet,
30
- } from '@juzi/wechaty-grpc'
31
-
32
- import {
33
- unpackFileBoxFromChunk,
34
- packFileBoxToChunk,
35
- } from './file-box-chunk.js'
36
- import { nextData } from './next-data.js'
37
-
38
- test('unpackFileBoxFromChunk()', async t => {
39
- const FILE_BOX_DATA = 'test'
40
- const FILE_BOX_NAME = 'test.dat'
41
-
42
- const stream = await getFileBoxStreamStub(FILE_BOX_DATA, FILE_BOX_NAME)
43
-
44
- const decodedFileBox = await unpackFileBoxFromChunk(stream)
45
- const data = (await decodedFileBox.toBuffer()).toString()
46
-
47
- t.equal(decodedFileBox.name, FILE_BOX_NAME, 'should get file box name')
48
- t.equal(data, FILE_BOX_DATA, 'should get file box data')
49
-
50
- })
51
-
52
- test('packFileBoxToChunk()', async t => {
53
- const FILE_BOX_DATA = 'test'
54
- const FILE_BOX_NAME = 'test.dat'
55
-
56
- const fileBox = FileBox.fromBuffer(
57
- Buffer.from(FILE_BOX_DATA),
58
- FILE_BOX_NAME,
59
- )
60
-
61
- const stream = await packFileBoxToChunk(fileBox)
62
-
63
- const fileBoxChunk = await nextData(stream)
64
- t.ok(fileBoxChunk.hasName(), 'has name')
65
-
66
- const fileName = fileBoxChunk.getName()
67
- t.equal(fileName, FILE_BOX_NAME, 'should get name')
68
-
69
- let data = ''
70
- stream.on('data', (chunk: puppet.FileBoxChunk) => {
71
- if (!chunk.hasData()) {
72
- throw new Error('no data')
73
- }
74
- data += chunk.getData()
75
- })
76
-
77
- await new Promise(resolve => stream.on('end', resolve))
78
-
79
- t.equal(data, FILE_BOX_DATA, 'should get file box data')
80
- })
81
-
82
- test('packFileBoxToChunk() <-> unpackFileBoxFromChunk()', async t => {
83
- const FILE_BOX_DATA = 'test'
84
- const FILE_BOX_NAME = 'test.dat'
85
-
86
- const fileBox = FileBox.fromBuffer(
87
- Buffer.from(FILE_BOX_DATA),
88
- FILE_BOX_NAME,
89
- )
90
-
91
- const stream = await packFileBoxToChunk(fileBox)
92
- const restoredBox = await unpackFileBoxFromChunk(stream)
93
-
94
- t.equal(fileBox.name, restoredBox.name, 'should be same name')
95
- t.equal(await fileBox.toBase64(), await restoredBox.toBase64(), 'should be same content')
96
- })
97
-
98
- test('should handle no name error in catch', async t => {
99
- t.plan(1)
100
- const FILE_BOX_DATA = 'test'
101
- const FILE_BOX_NAME = 'test.dat'
102
-
103
- const stream = await getFileBoxStreamStub(FILE_BOX_DATA, FILE_BOX_NAME, true)
104
-
105
- try {
106
- await unpackFileBoxFromChunk(stream)
107
- } catch (e) {
108
- t.equal((e as Error).message, 'no name')
109
- }
110
- })
111
-
112
- test('should handle first error catch', async t => {
113
- t.plan(1)
114
- const FILE_BOX_DATA = 'test'
115
- const FILE_BOX_NAME = 'test.dat'
116
-
117
- const stream = await getFileBoxStreamStub(FILE_BOX_DATA, FILE_BOX_NAME, false, true)
118
-
119
- try {
120
- await unpackFileBoxFromChunk(stream)
121
- } catch (e) {
122
- t.equal((e as Error).message, 'first exception')
123
- }
124
- })
125
-
126
- test('should handle middle error in further ops', async t => {
127
- t.plan(1)
128
- const FILE_BOX_DATA = 'test'
129
- const FILE_BOX_NAME = 'test.dat'
130
-
131
- const stream = await getFileBoxStreamStub(FILE_BOX_DATA, FILE_BOX_NAME, false, false, true)
132
-
133
- const fileBox = await unpackFileBoxFromChunk(stream)
134
- try {
135
- await fileBox.toBuffer()
136
- } catch (e) {
137
- t.equal((e as Error).message, 'middle exception')
138
- }
139
- })
140
-
141
- async function getFileBoxStreamStub (
142
- data: string,
143
- name: string,
144
- noname = false,
145
- firstException = false,
146
- middleException = false,
147
- ) {
148
- const fileBox = FileBox.fromBuffer(
149
- Buffer.from(data),
150
- name,
151
- )
152
-
153
- const stream: Duplex<puppet.FileBoxChunk, puppet.FileBoxChunk> = new PassThrough({ objectMode: true })
154
-
155
- if (firstException) {
156
- stream.pause()
157
- setImmediate(() => {
158
- stream.emit('error', new Error('first exception'))
159
- stream.resume()
160
- })
161
- } else {
162
- const chunk1 = new puppet.FileBoxChunk()
163
- if (!noname) {
164
- chunk1.setName(fileBox.name)
165
- }
166
- setTimeout(() => stream.write(chunk1), 10)
167
- }
168
-
169
- if (middleException) {
170
- setTimeout(() => {
171
- stream.emit('error', new Error('middle exception'))
172
- }, 100)
173
- }
174
-
175
- const fileBoxStream = await fileBox.toStream()
176
- fileBoxStream.on('data', chunk => {
177
- const fileBoxChunk = new puppet.FileBoxChunk()
178
- fileBoxChunk.setData(chunk)
179
- setTimeout(() => stream.write(fileBoxChunk), 200)
180
- })
181
- fileBoxStream.on('end', () => setTimeout(() => stream.end(), 250))
182
-
183
- return stream
184
- }
@@ -1,87 +0,0 @@
1
- import { FileBox } from 'file-box'
2
- import type { FileBoxInterface } from 'file-box'
3
- import { PassThrough } from 'stream'
4
- import {
5
- Readable,
6
- Transform,
7
- } from 'stronger-typed-streams'
8
-
9
- import { puppet } from '@juzi/wechaty-grpc'
10
-
11
- import { nextData } from './next-data.js'
12
-
13
- /**
14
- * @deprecated Will be removed after Dec 31, 2022
15
- */
16
- const decoder = () => new Transform<puppet.FileBoxChunk, any>({
17
- objectMode: true,
18
- transform: (chunk: puppet.FileBoxChunk, _: any, callback: any) => {
19
- if (!chunk.hasData()) {
20
- callback(new Error('no data'))
21
- return
22
- }
23
- const data = chunk.getData()
24
- callback(null, data)
25
- },
26
- })
27
-
28
- /**
29
- * @deprecated Will be removed after Dec 31, 2022
30
- */
31
- async function unpackFileBoxFromChunk (
32
- stream: Readable<puppet.FileBoxChunk>,
33
- ): Promise<FileBox> {
34
- const chunk = await nextData(stream)
35
- if (!chunk.hasName()) {
36
- throw new Error('no name')
37
- }
38
- const fileName = chunk.getName()
39
-
40
- const fileStream = new PassThrough({ objectMode: true })
41
- const transformedStream = stream.pipe(decoder())
42
- transformedStream.pipe(fileStream)
43
- stream.on('error', e => fileStream.emit('error', e))
44
- transformedStream.on('error', e => fileStream.emit('error', e))
45
-
46
- const fileBox = FileBox.fromStream(fileStream, fileName)
47
-
48
- return fileBox
49
- }
50
-
51
- /**
52
- * @deprecated Will be removed after Dec 31, 2022
53
- */
54
- const encoder = () => new Transform<any, puppet.FileBoxChunk>({
55
- objectMode: true,
56
- transform: (chunk: any, _: any, callback: any) => {
57
- const fileBoxChunk = new puppet.FileBoxChunk()
58
- fileBoxChunk.setData(chunk)
59
- callback(null, fileBoxChunk)
60
- },
61
- })
62
-
63
- /**
64
- * @deprecated Will be removed after Dec 31, 2022
65
- */
66
- async function packFileBoxToChunk (
67
- fileBox: FileBoxInterface,
68
- ): Promise<Readable<puppet.FileBoxChunk>> {
69
- const stream = new PassThrough({ objectMode: true })
70
-
71
- const chunk = new puppet.FileBoxChunk()
72
- chunk.setName(fileBox.name)
73
-
74
- // FIXME: Huan(202010) write might return false
75
- stream.write(chunk)
76
-
77
- fileBox
78
- .pipe(encoder())
79
- .pipe(stream)
80
-
81
- return stream
82
- }
83
-
84
- export {
85
- unpackFileBoxFromChunk,
86
- packFileBoxToChunk,
87
- }
@@ -1,31 +0,0 @@
1
- #!/usr/bin/env -S node --no-warnings --loader ts-node/esm
2
-
3
- import { test } from 'tstest'
4
-
5
- import { FileBox } from 'file-box'
6
-
7
- import {
8
- puppet,
9
- } from '@juzi/wechaty-grpc'
10
-
11
- import {
12
- packFileBoxToPb,
13
- unpackFileBoxFromPb,
14
- } from './file-box-pb.js'
15
-
16
- test('packFileBoxToPb()', async t => {
17
- const FILE_BOX_DATA = 'test'
18
- const FILE_BOX_NAME = 'test.dat'
19
-
20
- const fileBox = FileBox.fromBuffer(
21
- Buffer.from(FILE_BOX_DATA),
22
- FILE_BOX_NAME,
23
- )
24
-
25
- const pb = await packFileBoxToPb(puppet.MessageFileStreamResponse)(fileBox)
26
- const restoredFileBox = await unpackFileBoxFromPb(pb)
27
- t.ok(restoredFileBox instanceof FileBox, 'should get an instance of FileBOX')
28
-
29
- t.equal(restoredFileBox.name, fileBox.name, 'should get the right file box name')
30
- t.equal(await restoredFileBox.toBase64(), await fileBox.toBase64(), 'should get the right file box content')
31
- })
@@ -1,41 +0,0 @@
1
- import type { FileBoxInterface } from 'file-box'
2
- import type { Readable } from 'stronger-typed-streams'
3
-
4
- import {
5
- packFileBoxToChunk,
6
- unpackFileBoxFromChunk,
7
- } from './file-box-chunk.js'
8
- import {
9
- packFileBoxChunkToPb,
10
- unpackFileBoxChunkFromPb,
11
- } from './chunk-pb.js'
12
- import type { FileBoxPb } from './file-box-pb.type.js'
13
-
14
- /**
15
- * @deprecated Will be removed after Dec 31, 2022
16
- */
17
- function packFileBoxToPb<T extends FileBoxPb> (
18
- PbConstructor: { new(): T },
19
- ) {
20
- return async (fileBox: FileBoxInterface) => {
21
- const fileBoxChunkStream = await packFileBoxToChunk(fileBox)
22
- const pbFileBox = packFileBoxChunkToPb(PbConstructor)(fileBoxChunkStream)
23
- return pbFileBox
24
- }
25
- }
26
-
27
- /**
28
- * @deprecated Will be removed after Dec 31, 2022
29
- */
30
- async function unpackFileBoxFromPb<T extends FileBoxPb> (
31
- pbStream: Readable<T>,
32
- ): Promise<FileBoxInterface> {
33
- const fileBoxChunkStream = unpackFileBoxChunkFromPb(pbStream)
34
- const fileBox = await unpackFileBoxFromChunk(fileBoxChunkStream)
35
- return fileBox
36
- }
37
-
38
- export {
39
- packFileBoxToPb,
40
- unpackFileBoxFromPb,
41
- }
@@ -1,16 +0,0 @@
1
- import type { puppet } from '@juzi/wechaty-grpc'
2
-
3
- /**
4
- * Any Protocol Buffer message that include a FileBoxChunk
5
- */
6
- export interface FileBoxPb {
7
- hasFileBoxChunk(): boolean
8
- getFileBoxChunk(): puppet.FileBoxChunk | undefined
9
- setFileBoxChunk(value?: puppet.FileBoxChunk): void
10
- }
11
-
12
- export interface ConversationIdFileBoxPb extends FileBoxPb {
13
- hasConversationId(): boolean
14
- getConversationId(): string
15
- setConversationId(value: string): void
16
- }
@@ -1,8 +0,0 @@
1
- export {
2
- packConversationIdFileBoxToPb,
3
- unpackConversationIdFileBoxArgsFromPb,
4
- } from './conversation-id-file-box.js'
5
- export {
6
- packFileBoxToPb,
7
- unpackFileBoxFromPb,
8
- } from './file-box-pb.js'
@@ -1,35 +0,0 @@
1
- import type {
2
- Readable,
3
- } from 'stronger-typed-streams'
4
-
5
- /**
6
- * windmemory(20201027): generating fileBox data in server side might take longer.
7
- * Set to 60 sec to avoid unexpected timeout.
8
- */
9
- const TIMEOUT = 60 * 1000
10
-
11
- /**
12
- * @deprecated Will be removed after Dec 31, 2022
13
- */
14
- async function nextData<T> (
15
- stream: Readable<T>,
16
- ): Promise<T> {
17
- const chunk = await new Promise<T>((resolve, reject) => {
18
- const timer = setTimeout(reject, TIMEOUT)
19
- stream.once('data', chunk => {
20
- stream.pause()
21
- clearTimeout(timer)
22
-
23
- resolve(chunk)
24
- })
25
- stream.once('error', reject)
26
-
27
- })
28
-
29
- stream.resume()
30
- return chunk
31
- }
32
-
33
- export {
34
- nextData,
35
- }
@@ -1,24 +0,0 @@
1
- import {
2
- FileBox,
3
- FileBoxType,
4
- } from 'file-box'
5
- import type {
6
- FileBoxInterface,
7
- } from 'file-box'
8
-
9
- /**
10
- * @deprecated Will be removed after Dec 31, 2022
11
- */
12
- export const serializeFileBox = async (fileBox: FileBoxInterface): Promise<string> => {
13
- const serializableFileBoxTypes = [
14
- FileBoxType.Base64,
15
- FileBoxType.Url,
16
- FileBoxType.QRCode,
17
- ]
18
- if (serializableFileBoxTypes.includes(fileBox.type)) {
19
- return JSON.stringify(fileBox)
20
- }
21
- const base64 = await fileBox.toBase64()
22
- const name = fileBox.name
23
- return JSON.stringify(FileBox.fromBase64(base64, name))
24
- }