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.
Files changed (71) hide show
  1. package/CHANGELOG.md +27 -0
  2. package/build/DependencyResolver.d.ts +2 -1
  3. package/build/DependencyResolver.js +6 -5
  4. package/build/DependencyResolver.js.map +1 -1
  5. package/build/FileUploader.d.ts +2 -1
  6. package/build/FileUploader.js.map +1 -1
  7. package/build/Session.d.ts +6 -1
  8. package/build/Session.js +16 -7
  9. package/build/Session.js.map +1 -1
  10. package/build/State.js +2 -2
  11. package/build/State.js.map +1 -1
  12. package/build/WantedVersions.d.ts +30 -6
  13. package/build/WantedVersions.js +86 -26
  14. package/build/WantedVersions.js.map +1 -1
  15. package/build/defaultConfig.d.ts +2 -3
  16. package/build/defaultConfig.js +4 -4
  17. package/build/defaultConfig.js.map +1 -1
  18. package/build/index.d.ts +2 -2
  19. package/build/index.js +8 -8
  20. package/build/index.js.map +1 -1
  21. package/build/transports/CodeMessageBuilder.d.ts +1 -1
  22. package/build/transports/CodeMessageBuilder.js +2 -2
  23. package/build/transports/CodeMessageBuilder.js.map +1 -1
  24. package/build/transports/Protocol.d.ts +1 -1
  25. package/build/transports/Protocol.js.map +1 -1
  26. package/build/transports/Transport.d.ts +1 -0
  27. package/build/transports/Transport.js +6 -1
  28. package/build/transports/Transport.js.map +1 -1
  29. package/build/transports/types.d.ts +1 -1
  30. package/build/transports/types.js.map +1 -1
  31. package/build/transports/webPlayer.d.ts +2 -1
  32. package/build/transports/webPlayer.js.map +1 -1
  33. package/build/types.d.ts +10 -75
  34. package/build/types.js.map +1 -1
  35. package/build/utils.d.ts +2 -1
  36. package/build/utils.js.map +1 -1
  37. package/package.json +12 -4
  38. package/build/sdk.d.ts +0 -34
  39. package/build/sdk.js +0 -139
  40. package/build/sdk.js.map +0 -1
  41. package/build/sdks/features.d.ts +0 -9
  42. package/build/sdks/features.js +0 -13
  43. package/build/sdks/features.js.map +0 -1
  44. package/build/sdks/index.d.ts +0 -5
  45. package/build/sdks/index.js +0 -124
  46. package/build/sdks/index.js.map +0 -1
  47. package/build/sdks/types.d.ts +0 -21
  48. package/build/sdks/types.js +0 -3
  49. package/build/sdks/types.js.map +0 -1
  50. package/src/DependencyResolver.ts +0 -214
  51. package/src/DevSession.ts +0 -159
  52. package/src/FileUploader.ts +0 -133
  53. package/src/Logger.ts +0 -33
  54. package/src/Session.ts +0 -1280
  55. package/src/State.ts +0 -98
  56. package/src/WantedVersions.ts +0 -56
  57. package/src/defaultConfig.ts +0 -33
  58. package/src/index.ts +0 -31
  59. package/src/sdk.ts +0 -135
  60. package/src/sdks/features.ts +0 -11
  61. package/src/sdks/index.ts +0 -159
  62. package/src/sdks/types.ts +0 -49
  63. package/src/transports/CodeMessageBuilder.ts +0 -221
  64. package/src/transports/Protocol.ts +0 -78
  65. package/src/transports/Transport.ts +0 -270
  66. package/src/transports/index.ts +0 -8
  67. package/src/transports/types.ts +0 -88
  68. package/src/transports/webPlayer.ts +0 -91
  69. package/src/types.ts +0 -287
  70. package/src/utils.ts +0 -70
  71. package/tsconfig.json +0 -28
@@ -1,91 +0,0 @@
1
- import { SDKVersion, SnackWindowRef } from '../types';
2
- import {
3
- SnackTransport,
4
- SnackTransportOptions,
5
- SnackTransportListener,
6
- SnackTransportMessage,
7
- } from './types';
8
-
9
- export function createWebPlayerTransport(config: {
10
- name?: string;
11
- verbose?: boolean;
12
- webPlayerURL: string;
13
- window: Window;
14
- ref: SnackWindowRef;
15
- createTransport: (options: SnackTransportOptions) => SnackTransport;
16
- }): SnackTransport {
17
- let transport: SnackTransport | null = null;
18
- function getTransport(): SnackTransport {
19
- transport =
20
- transport ??
21
- config.createTransport({
22
- name: config.name ?? 'webplayer',
23
- verbose: config.verbose,
24
- });
25
- return transport;
26
- }
27
-
28
- let { origin } = new URL(config.webPlayerURL);
29
-
30
- let windowListener: ((event: any) => void) | undefined;
31
- let transportListener: ((event: any) => void) | undefined;
32
- return {
33
- addEventListener: (type: 'message', listener: SnackTransportListener) => {
34
- if (!windowListener) {
35
- windowListener = (event: any) => {
36
- if (event.origin === origin && typeof event.data === 'string') {
37
- if (transport) {
38
- transport.postMessage({
39
- type: 'synthetic_event',
40
- data: event.data,
41
- });
42
- }
43
- }
44
- };
45
- config.window?.addEventListener(type, windowListener);
46
- }
47
- if (!transportListener) {
48
- transportListener = (event: any) => {
49
- const message = event;
50
- if (message.type === 'synthetic_event') {
51
- config.ref.current?.postMessage(JSON.stringify(message.data), origin);
52
- } else {
53
- listener(message);
54
- }
55
- };
56
- }
57
- getTransport().addEventListener(type, transportListener);
58
- },
59
- removeEventListener: (type: 'message', _listener: SnackTransportListener) => {
60
- if (windowListener) {
61
- config.window?.removeEventListener(type, windowListener);
62
- windowListener = undefined;
63
- }
64
- if (transportListener) {
65
- transport?.removeEventListener(type, transportListener);
66
- transportListener = undefined;
67
- }
68
- },
69
- postMessage: (message: SnackTransportMessage) => getTransport().postMessage(message),
70
- // @ts-ignore: Dirty hack to allow snack web to to communicate with older web-players
71
- updateOrigin: (newOrigin: string) => (origin = newOrigin),
72
- };
73
- }
74
-
75
- export function getWebPlayerIFrameURL(
76
- webPlayerURL: string,
77
- sdkVersion: SDKVersion,
78
- initialURL: string,
79
- verbose: boolean
80
- ) {
81
- if (sdkVersion < '40.0.0' || typeof window === 'undefined') {
82
- return undefined;
83
- }
84
-
85
- return `${webPlayerURL.replace(
86
- '%%SDK_VERSION%%',
87
- sdkVersion.split('.')[0]
88
- )}/index.html?initialUrl=${encodeURIComponent(initialURL)}&origin=${encodeURIComponent(
89
- window.location.origin
90
- )}&verbose=${verbose}`;
91
- }
package/src/types.ts DELETED
@@ -1,287 +0,0 @@
1
- import { SDKVersion, SDKFeature } from './sdks/types';
2
- import { SnackTransport } from './transports';
3
-
4
- export type { SDKVersion, SDKFeature };
5
-
6
- /**
7
- * A non asset file that is included with the project.
8
- * This can be either a code file (.js/.tsx) or a support
9
- * file such as a markdown or json file.
10
- */
11
- export type SnackCodeFile = {
12
- type: 'CODE';
13
- contents: string;
14
- error?: Error;
15
- };
16
-
17
- /**
18
- * An asset file that refers to externaly available
19
- * content such as an image or font.
20
- *
21
- * When resolved, the `contents` field is an URL to the
22
- * uploaded asset. A File, Blob or FormData object may
23
- * also be provided after which it is automatically uploaded
24
- * and converted into an URL.
25
- */
26
- export type SnackAssetFile = {
27
- type: 'ASSET';
28
- contents: string | File | Blob | FormData; // string = url
29
- error?: Error;
30
- };
31
-
32
- /**
33
- * The content of a Snack code or asset file.
34
- */
35
- export type SnackFile = SnackCodeFile | SnackAssetFile;
36
-
37
- /**
38
- * Dictionary of file-names and their content that make up
39
- * the files of the Snack.
40
- */
41
- export type SnackFiles = {
42
- [path: string]: SnackFile;
43
- };
44
-
45
- /**
46
- * Dictionary of dependencies and their version.
47
- */
48
- export type SnackDependencyVersions = { [name: string]: string };
49
-
50
- /**
51
- * The version, resolved handle, peer-dependencies and optional
52
- * error of a dependency.
53
- */
54
- export type SnackDependency = {
55
- version: string;
56
- handle?: string;
57
- peerDependencies?: SnackDependencyVersions;
58
- error?: Error;
59
- wantedVersion?: string;
60
- };
61
-
62
- /**
63
- * Dictionary of dependency names and their (resolved) versions.
64
- */
65
- export type SnackDependencies = {
66
- [name: string]: SnackDependency;
67
- };
68
-
69
- /**
70
- * Wanted version of the dependency that is missing, including the
71
- * dependents which have this dependency as a peer-dependency.
72
- */
73
- export type SnackMissingDependency = {
74
- dependents: string[];
75
- wantedVersion?: string;
76
- };
77
-
78
- /**
79
- * Dictionary of dependencies that are missing.
80
- */
81
- export type SnackMissingDependencies = {
82
- [name: string]: SnackMissingDependency;
83
- };
84
-
85
- /**
86
- * An error that can optionally hold a file-name and line/column location.
87
- */
88
- export interface SnackError extends Error {
89
- fileName?: string;
90
- lineNumber?: number;
91
- columnNumber?: number;
92
- }
93
-
94
- export type SnackConnectedClientStatus = 'ok' | 'error' | 'reloading';
95
-
96
- /**
97
- * Client which connected to the Snack.
98
- *
99
- * Clients are only able to connect when the Snack is `online`.
100
- */
101
- export type SnackConnectedClient = {
102
- transport: string;
103
- id: string;
104
- name: string;
105
- platform: string;
106
- status: SnackConnectedClientStatus;
107
- error?: SnackError;
108
- previewURL?: string;
109
- previewTimestamp?: number;
110
- };
111
-
112
- /**
113
- * Collection of connected clients.
114
- */
115
- export type SnackConnectedClients = {
116
- [key: string]: SnackConnectedClient;
117
- };
118
-
119
- /**
120
- * User that is used for communicating with the Expo servers.
121
- */
122
- export type SnackUser = {
123
- sessionSecret?: string;
124
- accessToken?: string;
125
- };
126
-
127
- /**
128
- * Request data to be used with the `sendBeacon` API.
129
- */
130
- export type SnackSendBeaconRequest = {
131
- url: string;
132
- data: any;
133
- };
134
-
135
- export type SnackListenerSubscription = () => any;
136
-
137
- /**
138
- * Log data for when a connected client calls one of the
139
- * `console.[log,info,warn,error]` to log information to the
140
- * console.
141
- */
142
- export type SnackLogEvent = {
143
- type: 'error' | 'warn' | 'log' | 'info';
144
- message: string;
145
- connectedClient?: SnackConnectedClient;
146
- error?: SnackError;
147
- };
148
-
149
- export type SnackWindowRef = {
150
- current: Window | null;
151
- };
152
-
153
- export type SnackState = {
154
- /**
155
- * Expo SDK version.
156
- */
157
- sdkVersion: SDKVersion;
158
-
159
- /**
160
- * Files that make up the content (code & assets) of the Snack. There should
161
- * always be a file called "App.js" or "App.tsx" as the main entry point.
162
- */
163
- files: SnackFiles;
164
-
165
- /**
166
- * Packages that can be used in the code files. Packages that are pre-loaded
167
- * by the sdk may be ommited, but it is recommended to add them anyway.
168
- */
169
- dependencies: SnackDependencies;
170
-
171
- /**
172
- * Optional name. The name is used when saving or downloading the Snack; and is used
173
- * for the onlineName property.
174
- */
175
- name: string;
176
-
177
- /**
178
- * Additional description of the Snack. The description is used when saving the Snack
179
- * and may also be used for searching purposes.
180
- */
181
- description: string;
182
-
183
- /**
184
- * Collection of dependencies that are missing but are required by one or more of
185
- * the dependencies.
186
- */
187
- missingDependencies: SnackMissingDependencies;
188
-
189
- /**
190
- * Collection of packages and versions that are compatible with the selected
191
- * SDK version. This is similar to using `expo install`, which ensures the latest
192
- * compatible version is installed.
193
- */
194
- wantedDependencyVersions?: SnackDependencyVersions;
195
-
196
- /**
197
- * Disabled state. When the Snack is disabled it will not resolve any dependencies
198
- * or upload any asset files. It also disables the ability to go online.
199
- */
200
- disabled: boolean;
201
-
202
- /**
203
- * When online is true, Expo clients can connect to the Snack and receive live updates
204
- * when code or dependencies are changed. It also makes the Snack visible in the
205
- * "Recently in Development" section of the Expo client.
206
- */
207
- online: boolean;
208
-
209
- /**
210
- * Communication channel ("pubnub") through which live updates are transferred.
211
- * The communication channel is only used when the Snack is "online".
212
- */
213
- channel: string;
214
-
215
- /**
216
- * Device-id of the Expo client. When set causes the Snack to be visible in the
217
- * "Recently in Development" section of the Expo client with that device-id.
218
- * The device-id is only used when the Snack is "online".
219
- */
220
- deviceId?: string;
221
-
222
- /**
223
- * Unique experience url which can be used to open the Expo client and connect
224
- * to the Snack (e.g. "exp://exp.host/@snack/sdk.38.0.0-78173941").
225
- */
226
- url: string;
227
-
228
- /**
229
- * Name of the Snack as shown in the "Recently in Development" section in the
230
- * Expo client. The online-name will be empty when the Snack is not "online".
231
- */
232
- onlineName?: string;
233
-
234
- /**
235
- * A close request that should be send using the browser `sendBeacon` API
236
- * whenever the browser session is unloaded. This gives the Snack a last opportunity
237
- * to gracefully close its connections so that the "Recently in Development"
238
- * section in the Expo client no longer shows the Snack.
239
- */
240
- sendBeaconCloseRequest?: SnackSendBeaconRequest;
241
-
242
- /**
243
- * @internal
244
- */
245
- transports: { [id: string]: SnackTransport };
246
-
247
- /**
248
- * Clients that are currently connected.
249
- */
250
- connectedClients: SnackConnectedClients;
251
-
252
- /**
253
- *
254
- */
255
- user?: SnackUser;
256
-
257
- /**
258
- * Unsaved status of the Snack. Becomes `true` when the Snack code is changed and
259
- * `false` whenever the Snack is saved.
260
- */
261
- unsaved: boolean;
262
-
263
- /**
264
- * Id of the saved Snack.
265
- */
266
- id?: string;
267
-
268
- /**
269
- * URL of the saved Snack.
270
- * The URL is empty when no save "id" is available.
271
- */
272
- saveURL?: string;
273
-
274
- /**
275
- * Last saved (non-draft) Expo SDK version.
276
- */
277
- savedSDKVersion?: string;
278
-
279
- /**
280
- * URL to use to when loading the web-preview in an iframe.
281
- *
282
- * Web-preview is supported from SDK 40 and higher.
283
- * To enable it, set the `webPreviewRef` to the contentWindow
284
- * of the iframe.
285
- */
286
- webPreviewURL?: string;
287
- };
package/src/utils.ts DELETED
@@ -1,70 +0,0 @@
1
- import fetchPonyfill from 'fetch-ponyfill';
2
- import { customAlphabet } from 'nanoid';
3
-
4
- import { SDKVersion, SnackError, SnackUser } from './types';
5
-
6
- const { fetch } = fetchPonyfill();
7
- export { fetch };
8
-
9
- // + and - are used as delimiters in the uri, ensure they do not appear in the channel itself
10
- const VALID_CHANNEL_CHARS = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!_';
11
- const generateChannel = customAlphabet(VALID_CHANNEL_CHARS, 10);
12
-
13
- export function createChannel(channel?: string): string {
14
- channel = channel ?? generateChannel();
15
-
16
- if (channel.length < 6) {
17
- throw new Error('Please use a channel id with more entropy');
18
- }
19
-
20
- for (const char of channel) {
21
- if (VALID_CHANNEL_CHARS.indexOf(char) < 0) {
22
- throw new Error('Channel id contains invalid characters');
23
- }
24
- }
25
-
26
- return channel;
27
- }
28
-
29
- export function createURL(host: string, sdkVersion: SDKVersion, channel?: string, id?: string) {
30
- if (channel) {
31
- return id
32
- ? id.startsWith('@')
33
- ? `exp://${host}/${id}+${channel}`
34
- : `exp://${host}/@snack/${id}+${channel}`
35
- : `exp://${host}/@snack/sdk.${sdkVersion}-${channel}`;
36
- } else if (id) {
37
- return `exp://${host}/${id.match(/.*\/.*/) ? id : `@snack/${id}`}`;
38
- } else {
39
- return '';
40
- }
41
- }
42
-
43
- export function createError(config: {
44
- message: string;
45
- name?: string;
46
- fileName?: string;
47
- lineNumber?: number;
48
- columnNumber?: number;
49
- stack?: string;
50
- }): SnackError {
51
- const error: SnackError = new Error(config.message);
52
- if (config.name) error.name = config.name;
53
- if (config.fileName) error.fileName = config.fileName;
54
- if (config.lineNumber) error.lineNumber = config.lineNumber;
55
- if (config.columnNumber) error.columnNumber = config.columnNumber;
56
- if (config.stack) error.stack = config.stack;
57
- return error;
58
- }
59
-
60
- export function createUserHeader(user?: SnackUser): { [key: string]: string } {
61
- if (user?.sessionSecret) {
62
- return { 'Expo-Session': user.sessionSecret };
63
- }
64
-
65
- if (user?.accessToken) {
66
- return { Authorization: `Bearer ${user.accessToken}` };
67
- }
68
-
69
- return {};
70
- }
package/tsconfig.json DELETED
@@ -1,28 +0,0 @@
1
- {
2
- "compilerOptions": {
3
- "target": "es5",
4
- "module": "commonjs",
5
- "lib": ["dom", "esnext"],
6
- "declaration": true,
7
- "sourceMap": true,
8
- "inlineSources": true,
9
- "outDir": "./build",
10
- "strict": true,
11
- "noImplicitAny": true,
12
- "strictNullChecks": true,
13
- "strictFunctionTypes": true,
14
- "strictBindCallApply": true,
15
- "strictPropertyInitialization": true,
16
- "noImplicitThis": true,
17
- "noUnusedLocals": true,
18
- "noUnusedParameters": true,
19
- "noImplicitReturns": true,
20
- "noFallthroughCasesInSwitch": true,
21
- "allowSyntheticDefaultImports": true,
22
- "esModuleInterop": true,
23
- "moduleResolution": "node",
24
- "skipLibCheck": true,
25
- "forceConsistentCasingInFileNames": true
26
- },
27
- "include": ["./src"]
28
- }