snack-sdk 3.6.0 → 3.9.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.
- package/CHANGELOG.md +27 -0
- package/build/DependencyResolver.d.ts +2 -1
- package/build/DependencyResolver.js +6 -5
- package/build/DependencyResolver.js.map +1 -1
- package/build/FileUploader.d.ts +2 -1
- package/build/FileUploader.js.map +1 -1
- package/build/Session.d.ts +6 -1
- package/build/Session.js +16 -7
- package/build/Session.js.map +1 -1
- package/build/State.js +2 -2
- package/build/State.js.map +1 -1
- package/build/WantedVersions.d.ts +30 -6
- package/build/WantedVersions.js +86 -26
- package/build/WantedVersions.js.map +1 -1
- package/build/defaultConfig.d.ts +2 -3
- package/build/defaultConfig.js +4 -4
- package/build/defaultConfig.js.map +1 -1
- package/build/index.d.ts +2 -2
- package/build/index.js +8 -8
- package/build/index.js.map +1 -1
- package/build/transports/CodeMessageBuilder.d.ts +1 -1
- package/build/transports/CodeMessageBuilder.js +2 -2
- package/build/transports/CodeMessageBuilder.js.map +1 -1
- package/build/transports/Protocol.d.ts +1 -1
- package/build/transports/Protocol.js.map +1 -1
- package/build/transports/Transport.d.ts +1 -0
- package/build/transports/Transport.js +6 -1
- package/build/transports/Transport.js.map +1 -1
- package/build/transports/types.d.ts +1 -1
- package/build/transports/types.js.map +1 -1
- package/build/transports/webPlayer.d.ts +2 -1
- package/build/transports/webPlayer.js.map +1 -1
- package/build/types.d.ts +10 -75
- package/build/types.js.map +1 -1
- package/build/utils.d.ts +2 -1
- package/build/utils.js.map +1 -1
- package/package.json +12 -4
- package/build/sdk.d.ts +0 -34
- package/build/sdk.js +0 -139
- package/build/sdk.js.map +0 -1
- package/build/sdks/features.d.ts +0 -9
- package/build/sdks/features.js +0 -13
- package/build/sdks/features.js.map +0 -1
- package/build/sdks/index.d.ts +0 -5
- package/build/sdks/index.js +0 -124
- package/build/sdks/index.js.map +0 -1
- package/build/sdks/types.d.ts +0 -21
- package/build/sdks/types.js +0 -3
- package/build/sdks/types.js.map +0 -1
- package/src/DependencyResolver.ts +0 -214
- package/src/DevSession.ts +0 -159
- package/src/FileUploader.ts +0 -133
- package/src/Logger.ts +0 -33
- package/src/Session.ts +0 -1280
- package/src/State.ts +0 -98
- package/src/WantedVersions.ts +0 -56
- package/src/defaultConfig.ts +0 -33
- package/src/index.ts +0 -31
- package/src/sdk.ts +0 -135
- package/src/sdks/features.ts +0 -11
- package/src/sdks/index.ts +0 -159
- package/src/sdks/types.ts +0 -49
- package/src/transports/CodeMessageBuilder.ts +0 -221
- package/src/transports/Protocol.ts +0 -78
- package/src/transports/Transport.ts +0 -270
- package/src/transports/index.ts +0 -8
- package/src/transports/types.ts +0 -88
- package/src/transports/webPlayer.ts +0 -91
- package/src/types.ts +0 -287
- package/src/utils.ts +0 -70
- package/tsconfig.json +0 -28
|
@@ -1,221 +0,0 @@
|
|
|
1
|
-
import { createPatch } from 'diff';
|
|
2
|
-
import { UAParser } from 'ua-parser-js';
|
|
3
|
-
|
|
4
|
-
import FileUploader, { FileUploaderCallback } from '../FileUploader';
|
|
5
|
-
import { Logger } from '../Logger';
|
|
6
|
-
import { SnackIdentityState } from '../defaultConfig';
|
|
7
|
-
import { isModulePreloaded } from '../sdk';
|
|
8
|
-
import { SnackFiles, SDKVersion, SnackCodeFile, SnackDependencies } from '../types';
|
|
9
|
-
import { ProtocolCodeMessage, ProtocolCodeMessageDependencies } from './Protocol';
|
|
10
|
-
|
|
11
|
-
export type SnackCode = {
|
|
12
|
-
files: SnackFiles;
|
|
13
|
-
dependencies: SnackDependencies;
|
|
14
|
-
sdkVersion: SDKVersion;
|
|
15
|
-
};
|
|
16
|
-
|
|
17
|
-
const SnackIdentityCode: SnackCode = {
|
|
18
|
-
files: SnackIdentityState.files,
|
|
19
|
-
dependencies: SnackIdentityState.dependencies,
|
|
20
|
-
sdkVersion: SnackIdentityState.sdkVersion,
|
|
21
|
-
};
|
|
22
|
-
|
|
23
|
-
export type CodeMessageBuilderCallback = (codeMessage: ProtocolCodeMessage, code: SnackCode) => any;
|
|
24
|
-
|
|
25
|
-
export function getFileDiff(oldCode: string, newCode: string): string {
|
|
26
|
-
const patch = createPatch('code', oldCode, newCode, '', '', {
|
|
27
|
-
context: 0,
|
|
28
|
-
});
|
|
29
|
-
if (patch) {
|
|
30
|
-
return patch;
|
|
31
|
-
} else {
|
|
32
|
-
throw new Error('Error creating a file diff');
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
const PLACEHOLDER_URL =
|
|
37
|
-
'https://snack-code-uploads.s3.us-west-1.amazonaws.com/~asset/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
|
|
38
|
-
|
|
39
|
-
export default class CodeMessageBuilder {
|
|
40
|
-
private callback: CodeMessageBuilderCallback;
|
|
41
|
-
private code: SnackCode = SnackIdentityCode;
|
|
42
|
-
private codeMessage?: ProtocolCodeMessage;
|
|
43
|
-
private logger?: Logger;
|
|
44
|
-
private codeUploader?: FileUploader;
|
|
45
|
-
private maxDiffPlaceholder?: string;
|
|
46
|
-
private placeholderURLs: { [path: string]: string } = {};
|
|
47
|
-
private uploadedURLs: { [path: string]: string } = {};
|
|
48
|
-
private verifyCodeMessageSize: (message: ProtocolCodeMessage) => boolean;
|
|
49
|
-
|
|
50
|
-
constructor(options: {
|
|
51
|
-
callback: CodeMessageBuilderCallback;
|
|
52
|
-
verifyCodeMessageSize: (message: ProtocolCodeMessage) => boolean;
|
|
53
|
-
apiURL?: string;
|
|
54
|
-
logger?: Logger;
|
|
55
|
-
maxDiffPlaceholder?: string;
|
|
56
|
-
}) {
|
|
57
|
-
this.logger = options.logger;
|
|
58
|
-
this.callback = options.callback;
|
|
59
|
-
this.verifyCodeMessageSize = options.verifyCodeMessageSize;
|
|
60
|
-
this.maxDiffPlaceholder = options.maxDiffPlaceholder;
|
|
61
|
-
this.codeUploader = options.apiURL
|
|
62
|
-
? new FileUploader({
|
|
63
|
-
apiURL: options.apiURL,
|
|
64
|
-
logger: options.logger,
|
|
65
|
-
callback: this.onFileUploaded,
|
|
66
|
-
})
|
|
67
|
-
: undefined;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
setCode(code: SnackCode) {
|
|
71
|
-
if (code === this.code) {
|
|
72
|
-
return;
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
const prevCode = this.code;
|
|
76
|
-
const { files } = code;
|
|
77
|
-
this.code = code;
|
|
78
|
-
|
|
79
|
-
// Cancel any uploads for files that were removed or changed
|
|
80
|
-
for (const path in prevCode.files) {
|
|
81
|
-
if (prevCode.files[path].contents !== files[path]?.contents) {
|
|
82
|
-
delete this.placeholderURLs[path];
|
|
83
|
-
delete this.uploadedURLs[path];
|
|
84
|
-
this.codeUploader?.remove(path, prevCode.files[path]);
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
// Generate code message
|
|
89
|
-
const codeMessage = this.createCodeMessage(code, this.codeMessage, prevCode);
|
|
90
|
-
this.codeMessage = codeMessage;
|
|
91
|
-
|
|
92
|
-
// When the code message size is valid and there are no pending
|
|
93
|
-
// uploads, then fire the callback immediately
|
|
94
|
-
if (this.verifyCodeMessageSize(codeMessage)) {
|
|
95
|
-
if (!Object.keys(this.placeholderURLs).length) {
|
|
96
|
-
this.callback(codeMessage, code);
|
|
97
|
-
}
|
|
98
|
-
return;
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
// Upload files if the code message exceeds the limit
|
|
102
|
-
// Sort code files by size
|
|
103
|
-
if (this.codeUploader) {
|
|
104
|
-
const paths = Object.keys(files)
|
|
105
|
-
.filter((path) => files[path].type === 'CODE')
|
|
106
|
-
// @ts-ignore
|
|
107
|
-
.sort((a, b) => files[b].contents.length - files[a].contents.length);
|
|
108
|
-
|
|
109
|
-
// Upload the largest file
|
|
110
|
-
for (let i = 0; i < paths.length; i++) {
|
|
111
|
-
const path = paths[i];
|
|
112
|
-
if (!this.uploadedURLs[path] && !this.placeholderURLs[path]) {
|
|
113
|
-
const file: SnackCodeFile = files[path] as any;
|
|
114
|
-
this.placeholderURLs[path] = PLACEHOLDER_URL;
|
|
115
|
-
// this.logger?.comm('Uploading file', path, `, ${file.contents.length} bytes ...`);
|
|
116
|
-
this.codeUploader.add(path, file);
|
|
117
|
-
codeMessage.diff[path] = '';
|
|
118
|
-
codeMessage.s3url[path] = this.placeholderURLs[path];
|
|
119
|
-
if (this.verifyCodeMessageSize(codeMessage)) {
|
|
120
|
-
// Message size is now valid. When the upload completes, it
|
|
121
|
-
// will try to call the callback
|
|
122
|
-
return;
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
// Message is still too large
|
|
129
|
-
this.logger?.error('Message size is too large');
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
private createCodeMessage(
|
|
133
|
-
code: SnackCode,
|
|
134
|
-
prevCodeMessage?: ProtocolCodeMessage,
|
|
135
|
-
prevCode?: SnackCode
|
|
136
|
-
): ProtocolCodeMessage {
|
|
137
|
-
const { files, dependencies, sdkVersion } = code;
|
|
138
|
-
|
|
139
|
-
const diff: any = { ...files };
|
|
140
|
-
const s3url: any = {};
|
|
141
|
-
for (const path in files) {
|
|
142
|
-
const file = files[path];
|
|
143
|
-
if (this.uploadedURLs[path] || this.placeholderURLs[path]) {
|
|
144
|
-
diff[path] = '';
|
|
145
|
-
s3url[path] = this.uploadedURLs[path] || this.placeholderURLs[path];
|
|
146
|
-
} else {
|
|
147
|
-
if (file.type === 'CODE') {
|
|
148
|
-
if (
|
|
149
|
-
prevCodeMessage?.diff[path] &&
|
|
150
|
-
prevCode?.files[path].contents === code.files[path].contents
|
|
151
|
-
) {
|
|
152
|
-
diff[path] = prevCodeMessage.diff[path];
|
|
153
|
-
} else {
|
|
154
|
-
if (this.maxDiffPlaceholder && file.contents.length >= this.maxDiffPlaceholder.length) {
|
|
155
|
-
diff[path] = this.maxDiffPlaceholder;
|
|
156
|
-
} else {
|
|
157
|
-
diff[path] = getFileDiff('', file.contents);
|
|
158
|
-
}
|
|
159
|
-
}
|
|
160
|
-
} else {
|
|
161
|
-
diff[path] = '';
|
|
162
|
-
}
|
|
163
|
-
if (file.type === 'ASSET' && typeof file.contents === 'string') {
|
|
164
|
-
s3url[path] = file.contents;
|
|
165
|
-
}
|
|
166
|
-
}
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
const deps: ProtocolCodeMessageDependencies = {};
|
|
170
|
-
for (const name in dependencies) {
|
|
171
|
-
const dep = dependencies[name];
|
|
172
|
-
if (dep.handle && !isModulePreloaded(name, sdkVersion)) {
|
|
173
|
-
deps[name] = {
|
|
174
|
-
version: dep.version,
|
|
175
|
-
handle: dep.handle,
|
|
176
|
-
// Resolved has been replaced by handle. It is still needed for pre SDK 37 runtimes
|
|
177
|
-
resolved: dep.handle.substring(dep.handle.lastIndexOf('@') + 1),
|
|
178
|
-
};
|
|
179
|
-
}
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
const metadata: any = {
|
|
183
|
-
expoSDKVersion: code.sdkVersion,
|
|
184
|
-
// TODO: restore sdk version field
|
|
185
|
-
// webSnackSDKVersion: require('../../package.json').version,
|
|
186
|
-
};
|
|
187
|
-
if (typeof navigator !== 'undefined' && navigator.userAgent) {
|
|
188
|
-
const ua = new UAParser(navigator.userAgent).getResult();
|
|
189
|
-
metadata.webHostName = window.location.hostname;
|
|
190
|
-
metadata.webOSArchitecture = ua.cpu.architecture;
|
|
191
|
-
metadata.webOSFamily = ua.os.name;
|
|
192
|
-
metadata.webOSVersion = ua.os.version;
|
|
193
|
-
metadata.webLayoutEngine = ua.engine.name;
|
|
194
|
-
metadata.webDeviceType = ua.device.type;
|
|
195
|
-
metadata.webBrowser = ua.browser.name;
|
|
196
|
-
metadata.webBrowserVersion = ua.browser.version;
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
return {
|
|
200
|
-
type: 'CODE',
|
|
201
|
-
diff,
|
|
202
|
-
s3url,
|
|
203
|
-
dependencies: deps,
|
|
204
|
-
metadata,
|
|
205
|
-
};
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
private onFileUploaded: FileUploaderCallback = (request, resultURL, _error) => {
|
|
209
|
-
const { path } = request;
|
|
210
|
-
delete this.placeholderURLs[path];
|
|
211
|
-
if (resultURL) {
|
|
212
|
-
this.uploadedURLs[path] = resultURL;
|
|
213
|
-
if (!Object.keys(this.placeholderURLs).length) {
|
|
214
|
-
const codeMessage = this.createCodeMessage(this.code, this.codeMessage, this.code);
|
|
215
|
-
if (this.verifyCodeMessageSize(codeMessage)) {
|
|
216
|
-
this.callback(codeMessage, this.code);
|
|
217
|
-
}
|
|
218
|
-
}
|
|
219
|
-
}
|
|
220
|
-
};
|
|
221
|
-
}
|
|
@@ -1,78 +0,0 @@
|
|
|
1
|
-
import { SDKVersion } from '../sdks/types';
|
|
2
|
-
|
|
3
|
-
export type Device = {
|
|
4
|
-
name: string;
|
|
5
|
-
id: string;
|
|
6
|
-
platform: string;
|
|
7
|
-
};
|
|
8
|
-
|
|
9
|
-
export type ProtocolConsoleMessage = {
|
|
10
|
-
type: 'CONSOLE';
|
|
11
|
-
device: Device;
|
|
12
|
-
method: 'log' | 'error' | 'warn';
|
|
13
|
-
payload: any[];
|
|
14
|
-
};
|
|
15
|
-
|
|
16
|
-
export type ProtocolErrorMessage = {
|
|
17
|
-
type: 'ERROR';
|
|
18
|
-
error: string; // e.g. `{message: "my error", stack: "..."}`
|
|
19
|
-
device: Device;
|
|
20
|
-
};
|
|
21
|
-
|
|
22
|
-
export type ProtocolResendCodeMessage = {
|
|
23
|
-
type: 'RESEND_CODE';
|
|
24
|
-
device: Device;
|
|
25
|
-
};
|
|
26
|
-
|
|
27
|
-
export type ProtocolCodeMessageDependencies = {
|
|
28
|
-
[name: string]: {
|
|
29
|
-
version: string; // original version
|
|
30
|
-
resolved: string; // resolved version
|
|
31
|
-
handle: string; // snackager handle
|
|
32
|
-
};
|
|
33
|
-
};
|
|
34
|
-
|
|
35
|
-
export type ProtocolCodeMessage = {
|
|
36
|
-
type: 'CODE';
|
|
37
|
-
diff: { [path: string]: string };
|
|
38
|
-
s3url: { [path: string]: string };
|
|
39
|
-
dependencies: ProtocolCodeMessageDependencies;
|
|
40
|
-
metadata: {
|
|
41
|
-
expoSDKVersion: SDKVersion;
|
|
42
|
-
webSnackSDKVersion: string;
|
|
43
|
-
webHostname?: string; // window.location.hostname,
|
|
44
|
-
webOSArchitecture?: string; // os.architecture,
|
|
45
|
-
webOSFamily?: string; // os.family,
|
|
46
|
-
webOSVersion?: string; // os.version,
|
|
47
|
-
webLayoutEngine?: string; // platformInfo.layout,
|
|
48
|
-
webDeviceType?: string; // platformInfo.product,
|
|
49
|
-
webBrowser?: string; // platformInfo.name,
|
|
50
|
-
webBrowserVersion?: string; // platformInfo.version,
|
|
51
|
-
webDescription?: string; // platformInfo.description,
|
|
52
|
-
};
|
|
53
|
-
};
|
|
54
|
-
|
|
55
|
-
export type ProtocolReloadMessage = {
|
|
56
|
-
type: 'RELOAD_SNACK';
|
|
57
|
-
};
|
|
58
|
-
|
|
59
|
-
export type ProtocolRequestStatusMessage = {
|
|
60
|
-
type: 'REQUEST_STATUS';
|
|
61
|
-
};
|
|
62
|
-
|
|
63
|
-
export type ProtocolStatusMessage = {
|
|
64
|
-
type: 'STATUS_REPORT';
|
|
65
|
-
previewLocation: string;
|
|
66
|
-
status: 'FAILURE' | 'SUCCESS';
|
|
67
|
-
};
|
|
68
|
-
|
|
69
|
-
export type ProtocolOutgoingMessage =
|
|
70
|
-
| ProtocolCodeMessage
|
|
71
|
-
| ProtocolReloadMessage
|
|
72
|
-
| ProtocolRequestStatusMessage;
|
|
73
|
-
|
|
74
|
-
export type ProtocolIncomingMessage =
|
|
75
|
-
| ProtocolConsoleMessage
|
|
76
|
-
| ProtocolErrorMessage
|
|
77
|
-
| ProtocolResendCodeMessage
|
|
78
|
-
| ProtocolStatusMessage;
|
|
@@ -1,270 +0,0 @@
|
|
|
1
|
-
import PubNub from 'pubnub';
|
|
2
|
-
|
|
3
|
-
import { createLogger, Logger } from '../Logger';
|
|
4
|
-
import CodeMessageBuilder, { CodeMessageBuilderCallback } from './CodeMessageBuilder';
|
|
5
|
-
import { ProtocolOutgoingMessage, ProtocolIncomingMessage, ProtocolCodeMessage } from './Protocol';
|
|
6
|
-
import {
|
|
7
|
-
SnackTransport,
|
|
8
|
-
SnackTransportListener,
|
|
9
|
-
SnackTransportMessage,
|
|
10
|
-
SnackTransportOptions,
|
|
11
|
-
} from './types';
|
|
12
|
-
|
|
13
|
-
export function calcPubNubCodeMessageSize(channel: string, codeMessage: ProtocolCodeMessage) {
|
|
14
|
-
// https://support.pubnub.com/hc/en-us/articles/360051495932-Calculating-message-payload-size-before-publishing
|
|
15
|
-
return (
|
|
16
|
-
encodeURIComponent(
|
|
17
|
-
channel +
|
|
18
|
-
JSON.stringify(codeMessage).replace(
|
|
19
|
-
/[!~*'()]/g,
|
|
20
|
-
(x) => `%${x.charCodeAt(0).toString(16).toUpperCase()}`
|
|
21
|
-
)
|
|
22
|
-
).length + 200
|
|
23
|
-
);
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
export default class Transport implements SnackTransport {
|
|
27
|
-
private readonly channel: string;
|
|
28
|
-
private readonly logger?: Logger;
|
|
29
|
-
private callback?: SnackTransportListener;
|
|
30
|
-
private pubNub?: PubNub;
|
|
31
|
-
private codeMessageBuilder: CodeMessageBuilder;
|
|
32
|
-
private codeMessage?: ProtocolCodeMessage;
|
|
33
|
-
private connectionsCount: number = 0;
|
|
34
|
-
private readonly logSuffix: string;
|
|
35
|
-
|
|
36
|
-
constructor(options: SnackTransportOptions) {
|
|
37
|
-
const { apiURL, channel, verbose } = options;
|
|
38
|
-
this.channel = channel ?? '';
|
|
39
|
-
this.logger = verbose ? createLogger(true) : undefined;
|
|
40
|
-
this.logSuffix = options.name ? ` (${options.name})` : '';
|
|
41
|
-
|
|
42
|
-
if (channel) {
|
|
43
|
-
this.codeMessageBuilder = new CodeMessageBuilder({
|
|
44
|
-
verifyCodeMessageSize: this.onPubNubVerifyCodeMessageSize,
|
|
45
|
-
callback: this.onCodeMessageReady,
|
|
46
|
-
apiURL,
|
|
47
|
-
logger: this.logger,
|
|
48
|
-
maxDiffPlaceholder: 'X'.repeat(32768),
|
|
49
|
-
});
|
|
50
|
-
} else {
|
|
51
|
-
this.codeMessageBuilder = new CodeMessageBuilder({
|
|
52
|
-
verifyCodeMessageSize: () => true,
|
|
53
|
-
callback: this.onCodeMessageReady,
|
|
54
|
-
logger: this.logger,
|
|
55
|
-
});
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
addEventListener(_type: 'message', callback: SnackTransportListener) {
|
|
60
|
-
this.callback = callback;
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
removeEventListener(_type: 'message', _callback: SnackTransportListener) {
|
|
64
|
-
this.callback = undefined;
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
postMessage(message: SnackTransportMessage) {
|
|
68
|
-
switch (message.type) {
|
|
69
|
-
case 'start':
|
|
70
|
-
this.start();
|
|
71
|
-
break;
|
|
72
|
-
case 'stop':
|
|
73
|
-
this.stop();
|
|
74
|
-
break;
|
|
75
|
-
case 'update_code':
|
|
76
|
-
this.codeMessageBuilder.setCode(message.data);
|
|
77
|
-
break;
|
|
78
|
-
case 'protocol_message':
|
|
79
|
-
this.publish(message.data);
|
|
80
|
-
break;
|
|
81
|
-
case 'synthetic_event':
|
|
82
|
-
this.onSyntheticEvent(message.data);
|
|
83
|
-
break;
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
private start() {
|
|
88
|
-
this.stop();
|
|
89
|
-
|
|
90
|
-
if (this.channel) {
|
|
91
|
-
this.logger?.comm('Starting channel...', this.channel, this.logSuffix);
|
|
92
|
-
this.pubNub = new PubNub({
|
|
93
|
-
publishKey: 'pub-c-2a7fd67b-333d-40db-ad2d-3255f8835f70',
|
|
94
|
-
subscribeKey: 'sub-c-0b655000-d784-11e6-b950-02ee2ddab7fe',
|
|
95
|
-
ssl: true,
|
|
96
|
-
// uuid: TODO
|
|
97
|
-
presenceTimeout: 600,
|
|
98
|
-
heartbeatInterval: 60,
|
|
99
|
-
});
|
|
100
|
-
|
|
101
|
-
this.pubNub.addListener({
|
|
102
|
-
presence: this.onPubNubPresence,
|
|
103
|
-
message: this.onPubNubMessage,
|
|
104
|
-
status: this.onPubNubStatus,
|
|
105
|
-
});
|
|
106
|
-
|
|
107
|
-
this.pubNub.subscribe({
|
|
108
|
-
channels: [this.channel],
|
|
109
|
-
withPresence: true,
|
|
110
|
-
});
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
private stop() {
|
|
115
|
-
if (this.pubNub) {
|
|
116
|
-
this.logger?.comm('Stopping...', this.logSuffix);
|
|
117
|
-
this.pubNub.removeListener({
|
|
118
|
-
presence: this.onPubNubPresence,
|
|
119
|
-
message: this.onPubNubMessage,
|
|
120
|
-
status: this.onPubNubStatus,
|
|
121
|
-
});
|
|
122
|
-
|
|
123
|
-
this.pubNub.unsubscribe({
|
|
124
|
-
channels: [this.channel],
|
|
125
|
-
});
|
|
126
|
-
|
|
127
|
-
this.pubNub.stop();
|
|
128
|
-
this.pubNub = undefined;
|
|
129
|
-
}
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
private onProtocolMessage(connectionId: string, message: ProtocolIncomingMessage) {
|
|
133
|
-
if (message.type === 'RESEND_CODE') {
|
|
134
|
-
if (this.codeMessage) {
|
|
135
|
-
this.logger?.comm('Resending code...', this.logSuffix);
|
|
136
|
-
this.publish(this.codeMessage);
|
|
137
|
-
}
|
|
138
|
-
} else {
|
|
139
|
-
this.callback?.({
|
|
140
|
-
type: 'protocol_message',
|
|
141
|
-
connectionId,
|
|
142
|
-
data: message,
|
|
143
|
-
});
|
|
144
|
-
}
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
private async publish(message: ProtocolOutgoingMessage) {
|
|
148
|
-
if (!this.connectionsCount) {
|
|
149
|
-
return;
|
|
150
|
-
}
|
|
151
|
-
if (this.pubNub) {
|
|
152
|
-
try {
|
|
153
|
-
await this.pubNub.publish({
|
|
154
|
-
channel: this.channel,
|
|
155
|
-
message,
|
|
156
|
-
});
|
|
157
|
-
} catch (e) {
|
|
158
|
-
this.logger?.error('Failed to publish message', message.type, e, this.logSuffix);
|
|
159
|
-
}
|
|
160
|
-
} else {
|
|
161
|
-
this.callback?.({
|
|
162
|
-
type: 'synthetic_event',
|
|
163
|
-
data: message,
|
|
164
|
-
});
|
|
165
|
-
}
|
|
166
|
-
this.callback?.({
|
|
167
|
-
type: 'send_message',
|
|
168
|
-
data: message,
|
|
169
|
-
});
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
private onPubNubPresence = (event: PubNub.PresenceEvent) => {
|
|
173
|
-
switch (event.action) {
|
|
174
|
-
case 'join':
|
|
175
|
-
try {
|
|
176
|
-
const device = JSON.parse(event.uuid);
|
|
177
|
-
this.connectionsCount++;
|
|
178
|
-
this.callback?.({
|
|
179
|
-
type: 'connect',
|
|
180
|
-
connectionId: event.uuid,
|
|
181
|
-
data: device,
|
|
182
|
-
});
|
|
183
|
-
if (this.codeMessage) {
|
|
184
|
-
this.logger?.comm('Sending code...', this.logSuffix);
|
|
185
|
-
this.publish(this.codeMessage);
|
|
186
|
-
}
|
|
187
|
-
} catch (e) {
|
|
188
|
-
// Wasn't from the device
|
|
189
|
-
}
|
|
190
|
-
break;
|
|
191
|
-
case 'timeout':
|
|
192
|
-
case 'leave':
|
|
193
|
-
this.connectionsCount = Math.max(this.connectionsCount - 1, 0);
|
|
194
|
-
this.callback?.({
|
|
195
|
-
type: 'disconnect',
|
|
196
|
-
connectionId: event.uuid,
|
|
197
|
-
data: {
|
|
198
|
-
timedout: event.action === 'timeout',
|
|
199
|
-
},
|
|
200
|
-
});
|
|
201
|
-
break;
|
|
202
|
-
}
|
|
203
|
-
};
|
|
204
|
-
|
|
205
|
-
private onPubNubMessage = (event: PubNub.MessageEvent) => {
|
|
206
|
-
const message: ProtocolIncomingMessage = event.message;
|
|
207
|
-
this.onProtocolMessage(event.publisher, message);
|
|
208
|
-
};
|
|
209
|
-
|
|
210
|
-
private onPubNubStatus = (_event: PubNub.StatusEvent) => {
|
|
211
|
-
// TODO: Do something here?
|
|
212
|
-
};
|
|
213
|
-
|
|
214
|
-
private onPubNubVerifyCodeMessageSize = (codeMessage: ProtocolCodeMessage) => {
|
|
215
|
-
// Calculate unencoded size (quickly) and if that exceeds the limit
|
|
216
|
-
// then don't bother calculating the exact size (which is more expensive)
|
|
217
|
-
let approxSize = 0;
|
|
218
|
-
for (const path in codeMessage.diff) {
|
|
219
|
-
approxSize += path.length + codeMessage.diff[path].length;
|
|
220
|
-
}
|
|
221
|
-
if (approxSize >= 32768) {
|
|
222
|
-
return false;
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
// Calculate exact size and check whether it exceeds the limit
|
|
226
|
-
const size = calcPubNubCodeMessageSize(this.channel, codeMessage);
|
|
227
|
-
return size < 32768;
|
|
228
|
-
};
|
|
229
|
-
|
|
230
|
-
private onSyntheticEvent(data: any) {
|
|
231
|
-
if (typeof data !== 'string') {
|
|
232
|
-
return;
|
|
233
|
-
}
|
|
234
|
-
|
|
235
|
-
try {
|
|
236
|
-
const message = JSON.parse(data);
|
|
237
|
-
switch (message.type) {
|
|
238
|
-
case 'CONNECT':
|
|
239
|
-
this.connectionsCount++;
|
|
240
|
-
this.callback?.({
|
|
241
|
-
type: 'connect',
|
|
242
|
-
connectionId: JSON.stringify(message.device),
|
|
243
|
-
data: message.device,
|
|
244
|
-
});
|
|
245
|
-
break;
|
|
246
|
-
case 'DISCONNECT':
|
|
247
|
-
this.connectionsCount = Math.max(this.connectionsCount - 1, 0);
|
|
248
|
-
this.callback?.({
|
|
249
|
-
type: 'disconnect',
|
|
250
|
-
connectionId: JSON.stringify(message.device),
|
|
251
|
-
data: {
|
|
252
|
-
timedout: false,
|
|
253
|
-
},
|
|
254
|
-
});
|
|
255
|
-
break;
|
|
256
|
-
case 'MESSAGE':
|
|
257
|
-
this.onProtocolMessage(JSON.stringify(message.message.device), message.message);
|
|
258
|
-
break;
|
|
259
|
-
}
|
|
260
|
-
} catch (e) {
|
|
261
|
-
this.logger?.error('Failed to parse postMessage', e, data, this.logSuffix);
|
|
262
|
-
}
|
|
263
|
-
}
|
|
264
|
-
|
|
265
|
-
private onCodeMessageReady: CodeMessageBuilderCallback = (codeMessage) => {
|
|
266
|
-
this.codeMessage = codeMessage;
|
|
267
|
-
this.logger?.comm('Sending code...', this.logSuffix);
|
|
268
|
-
this.publish(codeMessage);
|
|
269
|
-
};
|
|
270
|
-
}
|
package/src/transports/index.ts
DELETED
package/src/transports/types.ts
DELETED
|
@@ -1,88 +0,0 @@
|
|
|
1
|
-
import { SnackFiles, SnackDependencies, SDKVersion } from '../types';
|
|
2
|
-
import { ProtocolOutgoingMessage, ProtocolIncomingMessage } from './Protocol';
|
|
3
|
-
|
|
4
|
-
export type SnackTransportStartMessage = {
|
|
5
|
-
type: 'start';
|
|
6
|
-
};
|
|
7
|
-
|
|
8
|
-
export type SnackTransportStopMessage = {
|
|
9
|
-
type: 'stop';
|
|
10
|
-
};
|
|
11
|
-
|
|
12
|
-
export type SnackTransportUpdateCodeMessage = {
|
|
13
|
-
type: 'update_code';
|
|
14
|
-
data: {
|
|
15
|
-
files: SnackFiles;
|
|
16
|
-
dependencies: SnackDependencies;
|
|
17
|
-
sdkVersion: SDKVersion;
|
|
18
|
-
};
|
|
19
|
-
};
|
|
20
|
-
|
|
21
|
-
export type SnackTransportProtocolMessage = {
|
|
22
|
-
type: 'protocol_message';
|
|
23
|
-
data: ProtocolOutgoingMessage;
|
|
24
|
-
};
|
|
25
|
-
|
|
26
|
-
export type SnackTransportSyntheticMessage = {
|
|
27
|
-
type: 'synthetic_event';
|
|
28
|
-
data: any;
|
|
29
|
-
};
|
|
30
|
-
|
|
31
|
-
export type SnackTransportMessage =
|
|
32
|
-
| SnackTransportStartMessage
|
|
33
|
-
| SnackTransportStopMessage
|
|
34
|
-
| SnackTransportUpdateCodeMessage
|
|
35
|
-
| SnackTransportProtocolMessage
|
|
36
|
-
| SnackTransportSyntheticMessage;
|
|
37
|
-
|
|
38
|
-
export type SnackTransportEventConnect = {
|
|
39
|
-
type: 'connect';
|
|
40
|
-
connectionId: string;
|
|
41
|
-
data: any; // DeviceInfo
|
|
42
|
-
};
|
|
43
|
-
|
|
44
|
-
export type SnackTransportEventDisconnect = {
|
|
45
|
-
type: 'disconnect';
|
|
46
|
-
connectionId: string;
|
|
47
|
-
data: {
|
|
48
|
-
timedout: boolean;
|
|
49
|
-
};
|
|
50
|
-
};
|
|
51
|
-
|
|
52
|
-
export type SnackTransportEventProtocolMessage = {
|
|
53
|
-
type: 'protocol_message';
|
|
54
|
-
connectionId: string;
|
|
55
|
-
data: ProtocolIncomingMessage;
|
|
56
|
-
};
|
|
57
|
-
|
|
58
|
-
export type SnackTransportEventSendMessage = {
|
|
59
|
-
type: 'send_message';
|
|
60
|
-
data: ProtocolOutgoingMessage;
|
|
61
|
-
};
|
|
62
|
-
|
|
63
|
-
export type SnackTransportEventSyntheticMessage = {
|
|
64
|
-
type: 'synthetic_event';
|
|
65
|
-
data: any;
|
|
66
|
-
};
|
|
67
|
-
|
|
68
|
-
export type SnackTransportEvent =
|
|
69
|
-
| SnackTransportEventConnect
|
|
70
|
-
| SnackTransportEventDisconnect
|
|
71
|
-
| SnackTransportEventProtocolMessage
|
|
72
|
-
| SnackTransportEventSendMessage
|
|
73
|
-
| SnackTransportEventSyntheticMessage;
|
|
74
|
-
|
|
75
|
-
export type SnackTransportListener = (event: SnackTransportEvent) => void;
|
|
76
|
-
|
|
77
|
-
export interface SnackTransport {
|
|
78
|
-
addEventListener(type: 'message', listener: SnackTransportListener): void;
|
|
79
|
-
removeEventListener(type: 'message', listener: SnackTransportListener): void;
|
|
80
|
-
postMessage(message: SnackTransportMessage): void;
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
export type SnackTransportOptions = {
|
|
84
|
-
channel?: string;
|
|
85
|
-
apiURL?: string;
|
|
86
|
-
verbose?: boolean;
|
|
87
|
-
name?: string;
|
|
88
|
-
};
|