@rsweeten/dropbox-sync 0.1.2 → 0.1.3

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 (32) hide show
  1. package/.github/workflows/test-pr.yml +30 -0
  2. package/README.md +207 -1
  3. package/__mocks__/nuxt/app.js +20 -0
  4. package/dist/adapters/__tests__/angular.spec.d.ts +1 -0
  5. package/dist/adapters/__tests__/angular.spec.js +237 -0
  6. package/dist/adapters/__tests__/next.spec.d.ts +1 -0
  7. package/dist/adapters/__tests__/next.spec.js +179 -0
  8. package/dist/adapters/__tests__/nuxt.spec.d.ts +1 -0
  9. package/dist/adapters/__tests__/nuxt.spec.js +145 -0
  10. package/dist/adapters/__tests__/svelte.spec.d.ts +1 -0
  11. package/dist/adapters/__tests__/svelte.spec.js +149 -0
  12. package/dist/core/__tests__/auth.spec.d.ts +1 -0
  13. package/dist/core/__tests__/auth.spec.js +83 -0
  14. package/dist/core/__tests__/client.spec.d.ts +1 -0
  15. package/dist/core/__tests__/client.spec.js +102 -0
  16. package/dist/core/__tests__/socket.spec.d.ts +1 -0
  17. package/dist/core/__tests__/socket.spec.js +122 -0
  18. package/dist/core/__tests__/sync.spec.d.ts +1 -0
  19. package/dist/core/__tests__/sync.spec.js +375 -0
  20. package/dist/core/sync.js +30 -11
  21. package/jest.config.js +24 -0
  22. package/jest.setup.js +38 -0
  23. package/package.json +4 -1
  24. package/src/adapters/__tests__/angular.spec.ts +338 -0
  25. package/src/adapters/__tests__/next.spec.ts +240 -0
  26. package/src/adapters/__tests__/nuxt.spec.ts +185 -0
  27. package/src/adapters/__tests__/svelte.spec.ts +194 -0
  28. package/src/core/__tests__/auth.spec.ts +142 -0
  29. package/src/core/__tests__/client.spec.ts +128 -0
  30. package/src/core/__tests__/socket.spec.ts +153 -0
  31. package/src/core/__tests__/sync.spec.ts +508 -0
  32. package/src/core/sync.ts +53 -26
package/src/core/sync.ts CHANGED
@@ -13,26 +13,26 @@ import type {
13
13
 
14
14
  // Define interface for Dropbox file entries
15
15
  interface DropboxFileEntry {
16
- '.tag': string;
17
- path_display?: string;
18
- [key: string]: any;
16
+ '.tag': string
17
+ path_display?: string
18
+ [key: string]: any
19
19
  }
20
20
 
21
21
  // Define interface for Dropbox API responses
22
22
  interface DropboxListFolderResponse {
23
23
  result: {
24
- entries: DropboxFileEntry[];
25
- has_more: boolean;
26
- cursor: string;
27
- };
24
+ entries: DropboxFileEntry[]
25
+ has_more: boolean
26
+ cursor: string
27
+ }
28
28
  }
29
29
 
30
30
  interface DropboxDownloadResponse {
31
31
  result: {
32
- fileBlob?: Blob;
33
- fileBinary?: string;
34
- [key: string]: any;
35
- };
32
+ fileBlob?: Blob
33
+ fileBinary?: string
34
+ [key: string]: any
35
+ }
36
36
  }
37
37
 
38
38
  export function createSyncMethods(
@@ -61,8 +61,13 @@ export function createSyncMethods(
61
61
 
62
62
  // If it's a Blob, convert it to a Buffer
63
63
  if (typeof Blob !== 'undefined' && data instanceof Blob) {
64
- const arrayBuffer = await data.arrayBuffer()
65
- return Buffer.from(arrayBuffer)
64
+ // In browser or when Blob has arrayBuffer method
65
+ if (data.arrayBuffer && typeof data.arrayBuffer === 'function') {
66
+ const arrayBuffer = await data.arrayBuffer()
67
+ return Buffer.from(arrayBuffer)
68
+ }
69
+ // Fallback for environments where arrayBuffer might not exist
70
+ return Buffer.from(String(data))
66
71
  }
67
72
 
68
73
  // If it's a plain ArrayBuffer
@@ -75,6 +80,17 @@ export function createSyncMethods(
75
80
  return Buffer.from(data)
76
81
  }
77
82
 
83
+ // For Jest mock objects that mimic Blob behavior
84
+ if (
85
+ data &&
86
+ typeof data === 'object' &&
87
+ 'arrayBuffer' in data &&
88
+ typeof data.arrayBuffer === 'function'
89
+ ) {
90
+ const arrayBuffer = await data.arrayBuffer()
91
+ return Buffer.from(arrayBuffer)
92
+ }
93
+
78
94
  // If we don't know what it is, throw an error
79
95
  throw new Error(`Unsupported data type: ${typeof data}`)
80
96
  }
@@ -124,10 +140,10 @@ export function createSyncMethods(
124
140
 
125
141
  async function fetchFolderContents(path: string) {
126
142
  try {
127
- const response = await dropbox.filesListFolder({
143
+ const response = (await dropbox.filesListFolder({
128
144
  path,
129
145
  recursive: true,
130
- }) as unknown as DropboxListFolderResponse
146
+ })) as unknown as DropboxListFolderResponse
131
147
 
132
148
  for (const entry of response.result.entries) {
133
149
  if (
@@ -151,9 +167,9 @@ export function createSyncMethods(
151
167
  }
152
168
 
153
169
  async function continueListing(cursor: string) {
154
- const response = await dropbox.filesListFolderContinue({
170
+ const response = (await dropbox.filesListFolderContinue({
155
171
  cursor: cursor,
156
- }) as unknown as DropboxListFolderResponse
172
+ })) as unknown as DropboxListFolderResponse
157
173
 
158
174
  for (const entry of response.result.entries) {
159
175
  if (
@@ -209,8 +225,9 @@ export function createSyncMethods(
209
225
  options?.localDir || path.join(process.cwd(), 'public', 'img')
210
226
  const dropboxDir = options?.dropboxDir || ''
211
227
 
212
- const localFiles = await scanLocalDirectory(localDir)
213
- const dropboxFiles = await scanDropboxFolder(dropboxDir)
228
+ // Use the public API methods instead of internal functions
229
+ const localFiles = await this.scanLocalFiles(localDir)
230
+ const dropboxFiles = await this.scanDropboxFiles(dropboxDir)
214
231
 
215
232
  // Normalize paths for comparison
216
233
  const normalizedDropboxFiles = dropboxFiles.map(normalizePath)
@@ -377,16 +394,18 @@ export function createSyncMethods(
377
394
  )
378
395
 
379
396
  // Download the file from Dropbox with proper error handling
380
- const response = await dropbox.filesDownload({
397
+ const response = (await dropbox.filesDownload({
381
398
  path: dropboxFilePath,
382
- }) as unknown as DropboxDownloadResponse
399
+ })) as unknown as DropboxDownloadResponse
383
400
 
384
401
  // Get file content - handle different possible response formats
385
- let fileContent: Buffer;
386
- const responseResult = response.result;
402
+ let fileContent: Buffer
403
+ const responseResult = response.result
387
404
 
388
405
  if (responseResult.fileBlob) {
389
- fileContent = await blobToBuffer(responseResult.fileBlob)
406
+ fileContent = await blobToBuffer(
407
+ responseResult.fileBlob
408
+ )
390
409
  } else if (responseResult.fileBinary) {
391
410
  fileContent = Buffer.from(
392
411
  responseResult.fileBinary,
@@ -395,11 +414,19 @@ export function createSyncMethods(
395
414
  } else {
396
415
  // For Node.js environment, Dropbox might return content in different properties
397
416
  const contentKey = Object.keys(responseResult).find(
398
- (key) => ['content', 'fileContent', 'file', 'data'].includes(key)
417
+ (key) =>
418
+ [
419
+ 'content',
420
+ 'fileContent',
421
+ 'file',
422
+ 'data',
423
+ ].includes(key)
399
424
  )
400
425
 
401
426
  if (contentKey) {
402
- fileContent = Buffer.from(responseResult[contentKey])
427
+ fileContent = Buffer.from(
428
+ responseResult[contentKey]
429
+ )
403
430
  } else {
404
431
  throw new Error(
405
432
  `Could not find file content in Dropbox response for ${dropboxFilePath}`