Skip to content

Commit

Permalink
opfs polyfill
Browse files Browse the repository at this point in the history
  • Loading branch information
Mazuh committed Nov 21, 2023
1 parent a03e005 commit bd9efcc
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 16 deletions.
73 changes: 73 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"preview": "vite preview"
},
"dependencies": {
"native-file-system-adapter": "^3.0.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"uuid": "^9.0.1"
Expand Down
40 changes: 25 additions & 15 deletions src/services/origin-private-file-system.test.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,37 @@
import { getOriginPrivateDirectory } from "native-file-system-adapter";
import { makeOpfsAdapter } from "./origin-private-file-system";

jest.mock("native-file-system-adapter", () => ({
getOriginPrivateDirectory: jest.fn(),
}));

describe("Origin private file system (OPFS) adapter", () => {
beforeAll(() => {
const getDirectoryMock = jest.fn().mockImplementation(() => ({
getFileHandle: jest.fn().mockImplementation(() => ({
getFile: jest.fn().mockResolvedValue({
text: jest
.fn()
.mockResolvedValue('{ "taste": "pepsicola", "surprise": false }'),
}),
createWritable: jest.fn().mockResolvedValue({
write: jest.fn(),
close: jest.fn(),
}),
})),
}));

Object.defineProperty(global.navigator, "storage", {
value: {
getDirectory: jest.fn().mockImplementation(() => ({
getFileHandle: jest.fn().mockImplementation(() => ({
getFile: jest.fn().mockResolvedValue({
text: jest
.fn()
.mockResolvedValue(
'{ "taste": "pepsicola", "surprise": false }'
),
}),
createWritable: jest.fn().mockResolvedValue({
write: jest.fn(),
close: jest.fn(),
}),
})),
})),
getDirectory: getDirectoryMock,
},
writable: true,
});

// if the polyfill gets removed, this line below must be removed too and that's it.
(getOriginPrivateDirectory as jest.Mock).mockImplementation(() =>
global.navigator.storage.getDirectory()
);
});

it("Integrates with real OPFS to retrieve parsed JSON content", async () => {
Expand Down
4 changes: 3 additions & 1 deletion src/services/origin-private-file-system.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { getOriginPrivateDirectory } from "native-file-system-adapter";

export interface OriginPrivateFileSystemAdapter<T> {
retrieve: () => Promise<T | null>;
persist: (data: T) => Promise<void>;
Expand All @@ -6,7 +8,7 @@ export interface OriginPrivateFileSystemAdapter<T> {
export async function makeOpfsAdapter<T>(
filename: string
): Promise<OriginPrivateFileSystemAdapter<T>> {
const opfsRoot = await navigator.storage.getDirectory();
const opfsRoot = await getOriginPrivateDirectory();
const fileHandle = await opfsRoot.getFileHandle(filename, {
create: true,
});
Expand Down
5 changes: 5 additions & 0 deletions src/vendor-types/native-file-system-adapter.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// See: https://github.com/jimmywarting/native-file-system-adapter/issues/50

declare module "native-file-system-adapter" {
export * from "native-file-system-adapter/types/mod";
}

0 comments on commit bd9efcc

Please sign in to comment.